Solved

php code problem

Posted on 2011-03-05
4
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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 …

717 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