Category Archives: tutorial

Let's do with Drupal – Simplified Admin Panel

As you (if you’v at least installed and explored Drupal) know, Drupal‘s Admin panel is not comfortable for clients. It’s mostly developer centric and not similar to other common content managing panels. But, sometimes clients may need to manage some type of content. Recently I’ve faced this situation and made a simple, generic admin panel that only offers managing selective contents. Now I am going to tell you the how I’v done it. If you follow the steps, it should take hardly 30 minutes.

Continue reading

Setting up Database testing with Kohana UnitTest module

Kohana Unittest module has made testing in Kohana v3 easy and fun. Lysender wrote a cool, straight forward post about how to set it up and running. I’ll just talk here about how I have set up database testing with Kohana Unittest for my personal use. Not saying “How to set up”. Because it’s obviously not the best way to do this.

Kohana Unittest

The important thing I need to ensure for testing database is, It’s in a known state before running a test. And, changes on database made by one test will not impact on another test. So, I’ll have a separate database for testing. Which will be loaded with a predefined dataset before running each test and will be truncated after test is done. Also, I have to be careful about that, my models are executing quires on test database in testing environment. Here is how I’ve done it? -

Continue reading

Extending Zend Form Element to create customized Phone number field

Generally, Phone numbers has different parts. Such as, country code, operator code and subscriber number etc. When taking Phone number as user input, we can worn users about phone number format by setting a hint/description and can validate using Regular Expression. But, for better usability, along with hints, some web applications provide separate (generally 3) text fields for different parts of phone number. Also, some of them keep a separator, ‘-’ or ‘#’ between this fields.

Now, if we try provide this feature in Zend Form, that’s possible. We can create three individual Zend_Form_Element_Text objects and join there value together to make the phone number. But, in this case, validating them together is a hassle. Again, placing them inline in the form and putting separators needs some Decorator magic. What if we can make it simply another Zend Form Element which renders three text box (optionally with separator)? And consider their value jointly and validate them together using ordinary Zend Validators? Lets try.

What we are going to make?

Let’s visualize our Phone number field first and list what we want from it.

  • It’s Simple. It’s rendering, validation everything is like any other ordinary Zend From Elements.
  • It prints 3 text boxes for different parts of Phone number.
  • When we validate it or take it’s value, the input of these three boxes jointly will be considered as the value.
  • We can set a separator character to be printed between text boxes.
  • We can include/exclude this separators from value.

Continue reading

Taking Zend Framework workshop on Dec, 09

Bbjobs has arranged a workshop on “Zend framework fundamental” on December, 09  and invited us (me and Emran Hasan) to take it. We agreed happily. This is (so far I know) the first formal Zend Framework workshop in Bangladesh. The workshop has been defined as -

The Zend Framework Workshop is a 5 half-day tutorial-style course that takes an in-depth look at Zend Framework and its components. The workshop is designed for experienced PHP programmers who want to learn to combine ZF concepts and structural elements to utilize the full power of this software development kit for PHP 5 applications.

As per our plan about the context of this workshop, we are going to cover the following 8 basic topics of Zend Framework in 5 days.

Continue reading

jQuery – Presentation at phpXperts Seminar, 09

At the phpXperts seminar 2009, I gave a talk on  “Blessings of modern javascript framework – jQuery” and it seemed well-received. I enjoyed giving the presentation very much!
Special thanks for all the organizers, group members and participants.

Here is the slide available for you.

Hope you’ll Enjoy the slides! Suggestions are welcome.

Advanced bootstrapping : Configure your Zend Framework application for multiple host

This tutorial is intended to explain a way of bootstrapping Zend Framework based MVC application for multiple host/domains. Before starting, let me explain a bit about the situation when it’s needed.

A web application goes through some stages when growing up. Generally, it starts from development and ends at production. There can be some more stages within this two ends. And, in maximum cases this stages are overlapped. The overlapped stages can be hosted in different servers with different settings but shares the same code. In this situations, if we want to keep bootstrap in SVN repo, it needs to setup bootstrap in a little different way. So that, it can handle many server settings with a single bootstrap.

For example, just now I am working on an application which is being developed in 3 local machines (me and two other members of my team) and being tested in our staging server. Besides, our client is monitoring progress of current sprint in their staging server and testing completed sprints in their pre-production server. Here, except the production servers, all hosts contains a checked out copy of code from a single repository. But this hosts have some difference in their library path, database host and authentication etc.

If my assumptions are ok for you, let’s start after downloading.

Assumptions


I have assumed that you are familiar with Zend Framework. And already worked on (atleast one) ZF MVC application. You know the basic things about bootstrapping and using the most common directory structure (shown in image). However, you can tune this technique for your favorite Zend directory stracture.

Download files

Downoad the files from here. The zip file contains -

  1. index.php – This is the bootstrap file. Your .htaccess file points all request to this file.
  2. config.xml – Contains all configurations of your application, for all hosts.
  3. Bootstrap.php – A php class that handles all startup settings in a vary organized way. (I don’t know who first wrote it, Thanks to him)

Continue reading

PunBB modification – Implementing iconized categories for forum topics

PunBB is one of the fastest and minimal forum softwares powered by PHP. PunBB says about itself:

PunBB is a fast and lightweight PHP-powered discussion board. It is released under the GNU General Public License. Its primary goals are to be faster, smaller and less graphically intensive as compared to other discussion boards. PunBB has fewer features than many other discussion boards, but is generally faster and outputs smaller, semantically correct XHTML-compliant pages.

You can see a detailed review about PunBB from forum-software.org.

What’s the purpose this mod?

PunBB has categories for forum. But there is no categorization for Topics like many other forums. This mod implements an iconized category for topics. The end result of this mod is -

  • It will add an excellent looking “Category Icons” fieldset in “Post new topic” form. (see Image#1)
  • Users can (optionally) select a topic category while posting a topic.
  • The category icon will be displayed beside the topic title in forum index page, topic detail page, search result etc. (see Image#2 and Image#3)
  • Clicking on a category icon will show all topics under that category.(see Image#4)

Lets see some screenshot of this modification :

Category icons in "Post new topic" form

Image#1:  Category icons in Post new topic form

Category icons before topic title in forum page

Image#2: Category icons before topic title in forum page

FireShot Pro capture #5 - 'User Contributed Deals _ Television is poluting our culture' - local_deals_com_forum_viewtopic_php_id=5

Image#3: Category icon in topic page

FireShot Pro capture #7 - 'User Contributed Deals _ Search results' - local_deals_com_forum_search_php_search_id=1345131291

Image#4: Filtering by category

Continue reading

Image manipulation in Zend Framework using PHP Thumbnailer Class v2.0

In the huge set of library, Zend Framework have no options for manipulating image. There is a proposal for Zend_Image which aimed to provides easier image manipulation using GD. But the proposal was not considered. Bill Karwin of ZF said,

Proposal not being considered

This proposal has been reviewed. The proposal describes functionality that is very similar to the existing PHP interfaces for ImageMagick or GD.

We are not considering this for inclusion in the Zend Framework at this time.

So, what to do when we need to make image thumbnail, resizeing or cropping image etc in Zend Framework?

The Solution

Zend Framework has the extensive power of integrating any library. So, we can use ImageMagick or GD library or any wrapper class using this libraries for manipulating image.

Here, I am explaining creating image thumbnail using PHP Thumbnailer Class v2.0 in 2 easy steps. I am assuming that, you are using Zend’s MVC with common directory structure and you’ve GD library with your PHP installation.

Step 1 – Create a Model for image manipulation :

Download the PHP5 version of PHP Thumbnailer Class v2.0 and put the class file “thumbnail.inc.php” in your model directory. Now change the file name to “Thumbnail.php” as it’s class name. Now your model is ready for action.

Step 2 – Use this model in Controller :

In your Controller, write a function to make thumbnails using this model. For example, you can use this function below:

<span style="color: #606060">   1:</span> <span style="color: #008000">/**</span>
<span style="color: #606060">   2:</span> <span style="color: #008000"> * Create thumbnail of an image</span>
<span style="color: #606060">   3:</span> <span style="color: #008000"> * </span>
<span style="color: #606060">   4:</span> <span style="color: #008000"> * @author    Anis uddin Ahmad (http://www.ajaxray.com)</span>
<span style="color: #606060">   5:</span> <span style="color: #008000"> * @param     string    Path of source image</span>
<span style="color: #606060">   6:</span> <span style="color: #008000"> * @param     string    Destination path of thumbnail</span>
<span style="color: #606060">   7:</span> <span style="color: #008000"> * @param     number    Width of thumbnail</span>
<span style="color: #606060">   8:</span> <span style="color: #008000"> * @param     number    Height of thumbnail</span>
<span style="color: #606060">   9:</span> <span style="color: #008000"> * @param     number    (optional) thumbnail image quality </span>
<span style="color: #606060">  10:</span> <span style="color: #008000"> */</span>
<span style="color: #606060">  11:</span>
<span style="color: #0000ff">private</span> <span style="color: #0000ff">function</span> _createThumbnail($sourcePath, $destPath, $w, $h, $q = 100)
<span style="color: #606060">  12:</span>  {
<span style="color: #606060">  13:</span>     $thumb = <span style="color: #0000ff">new</span> Thumbnail($sourcePath);
<span style="color: #606060">  14:</span>     $thumb-&gt;resize($w, $h);
<span style="color: #606060">  15:</span>     $thumb-&gt;save($destPath, $q);
<span style="color: #606060">  16:</span>  }

Done. Now you can use this function to resize/make thumbnail of an image. To resize an image with keeping it’s original name, pass the $sourcePath again in $destPath parameter. Then it will overwrite the existing image.

So, was it so though to manipulate image in ZF?

Join Problems with Zend_Paginator and Zend_Db_Select objects

Zend_Paginator is one of the newest members of Zend framework family. It’s surely a rich addition with Zend’s power. Moreover, It’s handling the main data of pagination, not only pagination links. Which I didn’t found in any other pagination library.

But, when I (and many other developers) start to working with this library, found many problems/bugs. The matter is, they are being solved very fast and hope to get a bullet-proof pagination library soon. For example, here is tow important fixings about Zend_Paginator and Zend_Db_Table_Select.

I had to face a problem with Zend_Paginator when using Join in Zend_Db_Select object. I am explaining here how the problem arises and how to solve it.

The Problem

One of the 4 Adapters for Zend_Paginator is DbSelect which uses a Zend_Db_Select instance. Now, the problem occurred if I need to have some calculative data from other tables and use join with the Zend_Db_Select object for them. For example, I am retrieving data from `deals` table and collecting number of comments from `comments` table and total votes from `votes` table. So, with other conditions, I had to add this lines with select object:

<span style="color: #606060">   1:</span> $select-&gt;joinLeft(array(<span style="color: #006080">'c'</span> =&gt; <span style="color: #006080">'comments'</span>), <span style="color: #006080">'deals.id = c.deal_id'</span>, array(<span style="color: #006080">'total_comments'</span>=&gt; <span style="color: #006080">'count(c.id)'</span>) );
<span style="color: #606060">   2:</span> $select-&gt;joinLeft(array(<span style="color: #006080">'v'</span> =&gt; <span style="color: #006080">'votes'</span>),    <span style="color: #006080">'delas.id = v.deal_id'</span>, array(<span style="color: #006080">'total_votes'</span>   =&gt; <span style="color: #006080">'count(v.id)'</span>) );
<span style="color: #606060">   3:</span> $select-&gt;group($<span style="color: #0000ff">this</span>-&gt;_name . <span style="color: #006080">'.id'</span>);

 

Now, the Zend_Paginator will always calculate the TotalRows as 1. That means, $this->totalItemCount; will always print 1 in “view partial“.

Why it happens?

Let’s take a look to the count function of DbSelect Adaptor of Zend_Paginator:

<span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> count()
<span style="color: #606060">   2:</span> {
<span style="color: #606060">   3:</span>     <span style="color: #0000ff">if</span> ($<span style="color: #0000ff">this</span>-&gt;_rowCount === <span style="color: #0000ff">null</span>) {
<span style="color: #606060">   4:</span>         $expression = <span style="color: #0000ff">new</span> Zend_Db_Expr(<span style="color: #006080">'COUNT(*) AS '</span> . self::ROW_COUNT_COLUMN);
<span style="color: #606060">   5:</span>         
<span style="color: #606060">   6:</span>         $rowCount   = clone $<span style="color: #0000ff">this</span>-&gt;_select;
<span style="color: #606060">   7:</span>         $rowCount-&gt;reset(Zend_Db_Select::COLUMNS)
<span style="color: #606060">   8:</span>                  -&gt;reset(Zend_Db_Select::ORDER)
<span style="color: #606060">   9:</span>                  -&gt;reset(Zend_Db_Select::LIMIT_OFFSET)
<span style="color: #606060">  10:</span>                  -&gt;columns($expression);
<span style="color: #606060">  11:</span>                  
<span style="color: #606060">  12:</span>         $<span style="color: #0000ff">this</span>-&gt;setRowCount($rowCount);
<span style="color: #606060">  13:</span>     }
<span style="color: #606060">  14:</span>&nbsp; 
<span style="color: #606060">  15:</span>     <span style="color: #0000ff">return</span> $<span style="color: #0000ff">this</span>-&gt;_rowCount;
<span style="color: #606060">  16:</span> }

Here, at line 7, the column list is being removed. But we have a `Group By` clause in our select object which is not cleared by this function and the result is always 1. At this point, if we add “->reset(Zend_Db_Select::GROUP)” after line 9, the calculation will get rid from always being 1. But there will arise another problem. That is, joined tables are still remaining in `From` clause. You can see them by adding “die(print_r($rowCount->getPart(Zend_Db_Select::FROM)));” after line 10. This join tables will make the calculation inconsistence because there is no expression columns now to summarize their result.

The Solution

Just alter this function as following to prevent this problem:

<span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">function</span> count()
<span style="color: #606060">   2:</span> {
<span style="color: #606060">   3:</span>     <span style="color: #0000ff">if</span> ($<span style="color: #0000ff">this</span>-&gt;_rowCount === <span style="color: #0000ff">null</span>) {
<span style="color: #606060">   4:</span>         $expression = <span style="color: #0000ff">new</span> Zend_Db_Expr(<span style="color: #006080">'COUNT(*) AS '</span> . self::ROW_COUNT_COLUMN);
<span style="color: #606060">   5:</span>        
<span style="color: #606060">   6:</span>         $rowCount   = clone $<span style="color: #0000ff">this</span>-&gt;_select;
<span style="color: #606060">   7:</span>         $rowCount-&gt;reset(Zend_Db_Select::FROM);
<span style="color: #606060">   8:</span>         
<span style="color: #606060">   9:</span>         <span style="color: #0000ff">foreach</span>($<span style="color: #0000ff">this</span>-&gt;_select-&gt;getPart(Zend_Db_Select::FROM) <span style="color: #0000ff">as</span> $name =&gt; $table)
<span style="color: #606060">  10:</span>         {
<span style="color: #606060">  11:</span>             <span style="color: #0000ff">if</span>(empty($table[<span style="color: #006080">'joinCondition'</span>]))        
<span style="color: #606060">  12:</span>             {
<span style="color: #606060">  13:</span>                 $rowCount-&gt;from(array($name =&gt; $table[<span style="color: #006080">'tableName'</span>]));
<span style="color: #606060">  14:</span>             }
<span style="color: #606060">  15:</span>             elseif($table[<span style="color: #006080">'joinType'</span>] == <span style="color: #006080">'inner join'</span>)        
<span style="color: #606060">  16:</span>
       {
<span style="color: #606060">  17:</span>                 $rowCount-&gt;join(array($name =&gt; $table[<span style="color: #006080">'tableName'</span>]), $table[<span style="color: #006080">'joinCondition'</span>]);
<span style="color: #606060">  18:</span>             }
<span style="color: #606060">  19:</span>         }
<span style="color: #606060">  20:</span>         
<span style="color: #606060">  21:</span>         $rowCount-&gt;reset(Zend_Db_Select::COLUMNS)
<span style="color: #606060">  22:</span>                  -&gt;reset(Zend_Db_Select::ORDER)
<span style="color: #606060">  23:</span>                  -&gt;reset(Zend_Db_Select::GROUP)
<span style="color: #606060">  24:</span>                  -&gt;reset(Zend_Db_Select::LIMIT_OFFSET)
<span style="color: #606060">  25:</span>                  -&gt;columns($expression);
<span style="color: #606060">  26:</span>&nbsp; 
<span style="color: #606060">  27:</span>         $<span style="color: #0000ff">this</span>-&gt;setRowCount($rowCount);
<span style="color: #606060">  28:</span>     }
<span style="color: #606060">  29:</span>&nbsp; 
<span style="color: #606060">  30:</span>     <span style="color: #0000ff">return</span> $<span style="color: #0000ff">this</span>-&gt;_rowCount;
<span style="color: #606060">  31:</span> }

At line 9, I am running a foreach which will remove the `from` clause and again assign only the main table and inner join tables. And, at line 23, just removing the `Group By` clause from query. Now, this function will provide right calculation even when there is some joins like this. But, if you use inner join in your Zend_Db_Select object (which is very rare for pagination), this function may not serve properly.

If there is any mistake in this alternation or any other way can make it better, please let me know. And… let’s meet power with simplicity.

UPDATE [26/08/08] : The function is updated and hopefully it will work fine for inner joins too.

UPDATE [24/09/08] : This issue is solved in ZF 1.6. Jurrien Stutterheim has reported about this post in Zend Framework Issue tracker and worked on it. After the release of 1.6, he requested me to check if this issue still exist. I’ve checked and found it solved. Thanks to Jurriën Stutterheim.

Developing web application with PHP-MySql-Apache in Ubuntu Linux

I have started using Ubuntu 8.04 in few days ago and found it Great. So, now I’ve moved my development environment from windows and working in Ubuntu. Here I am just explaining what steps I had to take for this jump. Also have a listing of some development related softwares/tools which I am using as replacement of windows applications.

Installing Apache, Php5, MySql in Ubuntu:

You can install Apache, Php, Mysql and other applications from Synaptic Package Manager. Go to System > Administration > Synaptic Package Manager and search with your desired application name. When found, double-click on package name. The package will be marked for installation along with it’s dependences. Now click on apply button to install the package. In this post, the red+bold texts are the name of software package which can be installed from Synaptic Package Manager.

To make your system ready for PHP development, you have to install more or less this packages: apache2.2-common, apache2-utils, php5, mysql-server-5.0, mysql-client-5.0, mysql-query-browser etc. You may also require some common php extensions which may not installed automatically with PHP installation. Most of them also will be found in Synaptic Package Manager as php5-gd, php5-curl, php5-mysql, php-pear etc.

Paths of important directories

One of the major differences I find here is the file system. All the directory paths I’ve been using in windows are changed here. I m listing here some PHP-MySql related paths here. But they may differ on your installation.

  • Apache configation files: /etc/apache2/
  • localhost root directory: /var/www/
  • PHP ini file: /etc/php5/apache2/
  • PHP extensions configuration files : /etc/php5/conf.d
  • MySql Data files: /var/lib/mysql/mysql
  • MySql Configuration files: /etc/mysql/

PHP Editors for Ubuntu

Gedit is the default text editor of Ubuntu and it support syntax coloring for many languages including PHP. But If you want an advanced editor for programming, you can try Geany or Screem. But, I am sure, who’ve been using advanced IDEs like PHPEd or PHPDesigner in windows, none of this can satisfy him. Don’t worry, the great open source IDE eclips has PHP-Development-Tool. It’s surely one of the best PHP IDE.

Subversion in Ubuntu

Subversion maintains current and historical versions of source code and documentation. It’s very important for distributed application development. RapidSVN is a graphical client for the subversion revision control system (svn) for linux. If you want to use svn from command line, install subversion.

File Difference and Merging Tools for Ubuntu

I have been using WinMerge in windows as file comparison and merging tool along with TortoiseSVN. Now using TkDiff as it’s alternative in linux. TkDiff has advanced functionalities and graphical interface. It provides file-merge and change-summary facilities, line number toggling (for easier cut & paste) and support for Subversion, RCS, CVS and SCCS. To use from command line, diff is may be the simplest file comparison tool.

Ftp and SSH

Ubuntu installs its default programs for FTP, SSH and many other internet applications. But, I’ve liked gFTP as FTP application because of it’s interface is similar to SmartFTP. And for working with SSH, required nothing. Just open the terminal and use ssh command. Write something like ssh username@example.com and press ENTER.

Testing in IE

One of the important (and painful to me) issue of web development is testing the output in IE. Though IE has no version for Linux, you can install it through wine. Wine is a application that crates a virtual environment for installing windows applications in Linux. After installing wine, you can install any available version of IE from http://www.tatanka.com.br/ies4linux/page/Installation:Ubuntu.

When witting this post, my aim was to help a developer who is thinking to switch to Ubuntu Linux or a newcomer here. You are welcome to add your valuable comment/suggestion for the same purpose.