BIRT reporting system for PHP web applications

BIRT is an Eclipse-based open source reporting system for web applications, especially those based on Java and J2EE. BIRT has two main components: a report designer based on Eclipse, and a runtime component (Birt Viewer) that you can add to your app server. [ref: http://www.eclipse.org/birt/phoenix/intro/]

Although BIRT is designed to be integrated into a J2EE web application, you can use it in any other development environment such as PHP. A brief description about how it works is given below.

To design the reports, BIRT designer should be installed. There are few ways to do that, and those are described in – http://www.eclipse.org/birt/phoenix/build/#introduction . Designing reports is extremely easy, and tools are there to bind data with database tables/fields, build queries etc..

The next thing is, there should be a instance of BIRT report viewer installed in a web server like tomcat. How to install BIRT viewer in tomcat is described here – http://www.eclipse.org/birt/phoenix/deploy/viewerSetup.php#install_jdbc (BIRT viewer is the tool that you used to view the reports). After installing the viewer, report design files can be hosted there.

Now you can request the report . Given below is a very basic example, which passes only one parameter to the Report Viewer. How PHP applications can use BIRT is described here – http://www.eclipse.org/birt/phoenix/deploy/usingPHP.php#dynamic .

     /*
     * @param sfWebRequest $request
     */
    public function executeTestReport(sfWebRequest $request){
 
        $repName = "new_report.rptdesign";
        // Redirect browser
        $dest = "http://localhost:8080/birt-viewer/frameset?__report={$repName}";
        header("Location: $dest" );
        die();
    }

http://www.eclipse.org/birt/phoenix/tutorial/ – This Basic Tutorial walks you though the steps to build a basic listing report and introduces the basic BIRT concepts.

How to split dictionary files in Symfony

Mainly, there are two ways to split the translation files in symfony.

01)

You can split the translation file into several dictionary files, with a certain name. For example, you can split the messages.si.xml file into these two (or more) files in the application i18n/ directory:

  • main.si.xml
  • sub.si.xml

When a translation is not to be found in the default messages.si.xml file, you must declare which dictionary is to be used each time you call the __() helper, using its third argument. For example, to output a string that is translated in the sub.si.xml dictionary, write this:

 <?php echo __('something', null, 'sub') ?>

02)

The other way of organizing translation dictionaries is to split them by module. Instead of writing a single messages.xx.xml file for the whole application, you can write one in each “modules/[module_name]/i18n/” directory.

Ex:

  • symfony/apps/frontend/modules/NewModule/i18n/messages.si.xml
  • symfony/apps/frontend/modules/NewModule/i18n/messages.en.xml
  • symfony/apps/frontend/modules/NewModule/i18n/messages.ta.xml

If you have the same translation element in both main messages file and module specific messages file, the priority will be given to the module specific file’s element.

Common Characteristics of Enterprise Applications

Last week, I was conducting a tech talk about “Architectural Patterns of Enterprise Applications” with our team. The discussion was mainly based on Marin Fowler’s famous book “Patterns of Enterprise Application Architecture”. So, I thought, it’s good to write something about that in my Blog. Given below are few common characteristics of Enterprise Applications. If any software product has the following characteristics, we can identify it as an Enterprise Application. These ware originally documented by “Martin Fowler”, in his book “Patterns of Enterprise Application Architecture”.

  • Persistent Data – Enterprise applications usually involve persistent data. The data is persistent because it needs to be around between multiple runs of the program—indeed, it usually needs to persist for several years. Also during this time there will be many changes in the programs that use it.
  • Lot of Data – There’s usually a lot of data, a moderate system will have over 1 GB of data organized in tens of millions of records—so much that managing it is a major part of the system.
  • Access Data Concurrently – Usually many people access data concurrently. For many systems this may be less than a hundred people, but for Web-based systems that talk over the Internet this goes up by orders of magnitude.
  • Lot of User Interface Screens – With so much data, there’s usually a lot of user interface screens to handle it. It’s not unusual to have hundreds of distinct screens.
  • Integrate with other Enterprise Applications – Enterprise applications rarely live on an island. Usually they need to integrate with other enterprise applications scattered around the enterprise. The various systems are built at different times with different technologies, and even the collaboration mechanisms will be different.

Special characters and text fields

Yesterday I was fixing a bug in one of our customization projects. There was a web form with some input fields. The bug was, when you enter some text in a text field with double quotes (“) data is not saving properly. Actually, the problem was in updating the records. All the characters that is there after the first double quote is truncated. Actually, the fix was very simple. This is something that you may find in any HTML based user interface, and you can fix this within few seconds if you know the exact reason for the bug. The reason for the bug is explained bellow.

In a text field for example, say, we enter a value like – abc”de. Then, you save the form and send this to the data base. Now the value – abc”de – is in a row of a database table, that’s okay. After that, if you go to edit the same record, you can see that value in the text field as – abc. The part after the double quote is missing. Usually, in a text box, value is kept in the “value” attribute within double quotes. After we keep some value that contains double quotes, within the value attribute of a input tag, it should looks like given below.

<input name="txtEventName" type="text" value="abc”de" />

This thing happens only when you get the value from a database table. If you type it manually in the text box, there will be the relevant HTML entities for the double quotes. That should looks like given below.

<input name="txtEventName" type="text" value="abc&#38;de" />

or

<input name="txtEventName" type="text" value="abc&quote;de" />

To fix the bug, what we should do is to replace the special characters such as double quotes from the values taken from database. In languages like PHP, this is really simple. You can just use a function such as “htmlentities()”. See the code given below.

<input type="text" name="txtFieldEventName" value="<?php echo htmlentities($notification->getEventName()); ?>" />

Importance of learning Mathematics for Computing

Today, many IT under graduates don’t see the importance of learning mathematics needed for computing. And also there may not any compulsory subjects for mathematics in some degree programs. Basically, learning some areas of Pure Mathematics is very important, if you want to become a real Software Engineer. Actually, when I was schooling, I also didn’t know how important it is to learn these maths stuff for the job I am hoping to do in future. So those days, I had no any motivation. But after starting to follow an IT degree, there were some compulsory maths subjects for us, and then I had the motivations to learn them because there was some awareness in me about how these things are used in real life. According to my point of view, this is one of the biggest problems in Sri Lanka’s school education system, specially in advanced level, no body knows what the reason for learning these subjects. But some times, there are reasons to be happy about not getting an effort to learn some subject areas in schooling time. For an example, the developers who specialized in Web based Enterprise Applications, Web Application Frameworks etc need not to know anything about Physics, Chemistry, and some areas of Applied Mathematics. But for them, it is extremely important to learn some areas of Pure Mathematics. That is the reason for there are modules for mathematics subjects in many good IT degree programs. Given below is a very simple example to prove this fact. This is the simplest one I could found in my recent works.

In one of our projects, there is an automatic leave allocation system. In that system there are so many things such as monthly allocation of leave, leave accumulations over the years, according to the employees job titles, pay grades, employment statuses, different leave types etc. And, there is a rule, no one can exceed the remaining vacation leave amount more than seventy days. This thing is checked always when assigning leave, copying and bringing forward the leave from previous year. Remaining vacation leave amount is calculated as given below.

remaining_amount = (allocated_amount + brought_forward) - taken_amount

When allocating leave, following simple condition is used to check whether the new leave amount exceeds seventy days.

(new_vacation_amount + brought_forward) - taken_amount >= 70

Now, when allocating vacation leave if the new added amount makes the remaining amount more than seventy, then the new amount should be adjusted so that remaining amount not exceeds seventy. This adjusted value can be calculated simply using the same function given above.

(new_vacation_amount + brought_forward) - taken_amount = 70


new_vacation_amount + brought_forward = 70 + taken_amount


new_vacation_amount = (70 + taken_amount) - brought_forward --->(3)

So, using the third equation, new vacation leave amount can be calculated when the new amount needs to be adjusted to keep remaining amount less than or equal seventy days. Actually, to do a simple things like this, there may be no need of a formal education of mathematics. But in some more complex situations the development time can be reduced by using the knowledge of mathematics. And, also I am not telling here every one needs to have a good maths knowledge to do the things. We all know, there are (were/will be) so many individuals who changed the world by doing good things without having any formal education.

Add slashes, strip slashes, and magic quotes…

Today, I was fixing some bug related to text processing in one of our projects. When saving a text that was typed in a text area, for some characters, additional slashes are added.

Ex: TEST\TEST becomes TEST\\TEST

This was because, the “magic quotes gpc” setting is turned on, and the slashes are not striped after retrieving the saved data. When you saving the data again and again, additional slashes are added due to this situation. Actually, these slashes should be added before you’re entering data into a database. The “magic quotes gpc” does it automatically in PHP. You can turn this setting off, and do it manually using the “addslashes” function as well. After all, it is advisable to write your code so that it handles both of these situations. Something like given below can be used for this.

if (!get_magic_quotes_gpc()) {
    $notification->setSubject(addslashes($subject));
    $notification->setContent(addslashes($content));
} else {
    $notification->setSubject($subject);
    $notification->setContent($content);
}

When retrieving data, slashes can be striped using the “stripslashes” function.

    $subject = stripslashes($notification->getSubject());
    $content = stripslashes($notification->getContent());

Concatenating a string to a class constant in PHP

Today I wanted to concatenate a String value to a Class constant to get some task done. I wrote a line as follows.

1
2
3
4
class DeadlineNotifications extends AppraisalObject {
 
         const ROOT_PATH = '/var/www/dev/nus';
         const EMAIL_TEMPLATE_DIR = ROOT_PATH . '/lib/models/performance/emailTemplates/';

Expected value in second constant:
—————-
/var/www/dev/nus/lib/models/performance/emailTemplates/

Actual result:
————–
Parse error: syntax error, unexpected ‘.’, expecting ‘,’ or ‘;’

In a way, some one can consider this thing as a bug of PHP. But this is not a bug. You can only initialize class constants with constant values. Here I have passed a statement which has to be evaluated at runtime. But the class constants are defined at compile time which comes earlier.

Test Driven Merging

Screenshot-11

Few weeks a go, I was doing some source code merging works. Some features developed in some branches of the repository were merged into some other branches. For the simple merging tasks, we directly use the “svn merge” command. But, in most of the times, we can’t achieve what we need just using the “svn merge”. That is, because the branches we do the merging between, have been grown largely, and independently.

For these situations, we have to use a good merging tool such as “Meld”. Actually, this is the best tool I have ever seen for this task.

“Meld is a visual diff and merge tool. You can compare two or three files and edit them in place (diffs update dynamically). You can compare two or three folders and launch file comparisons. You can browse and view a working copy from popular version control systems such such as CVS, Subversion, Bazaar-ng and Mercurial.” [http://meld.sourceforge.net/]

When you merge some thing did by other person in some other branch, there may be some difficulties, because you may don’t know the exact files and places to add the modifications. In this type of situations, it is easy to start the merging from known place such as an UI. After merging that known place, you can run the application, and through the error messages you get, you can find all the other places that are to be merged. Usually, you can do this by starting in a place such as an UI and it will end up in a place such as a data base table eventually. Although this method is very simple, I found it is very effective, and you can finish the whole process in a less time. This is a test driven way, so I would like to call it as “Test Driven Merging”.

Patterns of Enterprise Application Architecture

14266056These days, I am reading the book “Patterns of Enterprise Application Architecture – Martin Fowler”.

In the introduction of the book there are some definitions about the terms such as Patterns, Enterprise Applications, and Architecture etc. The features of Enterprise software, and how they differ from other types of software are described in detail.

After the introduction, there are two main sections. The Narratives, and Patterns. The first section is a short tutorial on developing enterprise applications. The next section, is a detailed reference to the patterns themselves.

At the moment, I finished reading the Introduction and willing to start reading the first section.

CakePHP and composite primary keys

Today, I was doing some comprehensive research about CakePHP framework, as a part of our web application framework research. I installed it and developed a sample application following the official documentation. Some interesting facts were found. And, here, I am listing down few of the advantages and disadvantages of this framework in relation to our existing systems.

CakePHP has many good characteristics such as easy configuration, easy installation, light weight etc. And also, it’s a excellent RAD tool, and it will save development time in a great margin. The database tasks can be done without writing one single sql query. And, CakePHP can be used with or without apache rewrite module, there is a clean url mechanism inside the framework instead of url rewriting.

Naming convention is very important in CakePHP. Database tables should have the same name as the relevant model classes. For example, by naming the model “Orange”, CakePHP can automatically infer that this model will be used in the “OrangeController”, and will be tied to a database table called “Oranges”.

Composite primary keys can’t be used for data base tables. Even if composite primary keys are kept in the tables, those are not used by the framework. Framework uses only one field as the primary key. If you want to use composite primary keys, you should hand write all the queries related to that task.

Alternatively, If we want to add a existing table that have two fields which compose the primary key, then what we have to do is we should add a new ‘id’ field as the primary key, and that field will be used by framework, we can make the other two fields (previous composite primary keys) as unique keys.

After all, the fact that the composite primary keys can’t be used with CakePHP is clearly mentioned in the official documentation as well.