iphp::blog()

JqGrid and Zend Framework

31 comments

Over the past month I have been working on integrating the jQuery Grid Plugin jqGrid into Zend Framework, for mostly internal use on company logistics systems. If you haven’t used jqGrid before, its a client-side AJAX grid based on JQuery with a staggering feature set covering everything you could imagine, just to name a few paging, sorting, searching, row editing, subgrids and more.

This release has only been possible with the kind permission from Warrant Group Ltd, to publish the code and give back to Zend Framework community. Especially after the proposal for ZendX_JQuery_JqGrid has laid dorment for nearly a year now, it was time to give this a slight nudge.

Also would just like to say thanks to Benjamin Eberlei for the creating the ZendX_JQuery libraries, it has been an absolute pleasure to work with your code.

Download

This library is currently in ALPHA and is not being used in production, but in the true style of Agile Development i am going to release early and often, the PHPunit tests, documentation and a new draft of a proposal for Zend Framework will come soon.

Browse Source Code

Download Sandbox Example

Example


<?php

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
		$books = array(
			array(	'title' => 'PHP Objects, Patterns, and Practice (2nd edition)',
					'author' => 'Matt Zandstra',
					'purchased' => '2006-05-01'),

			array(	'title' => 'Patterns of Enterprise Application Architecture',
					'author' => 'Martin Fowler',
					'purchased' => '2007-08-10'),

			array(	'title' => 'Domain Driven Design: Tackling Complexity in the Heart of Software',
					'author' => 'Eric Evans',
					'purchased' => '2009-02-06')
		);

		 $grid = new Ingot_JQuery_JqGrid('bookshelf',
		             new Ingot_JQuery_JqGrid_Adapter_Array($books));

		 $grid->addColumn(new Ingot_JQuery_JqGrid_Column('title'));
		 $grid->addColumn(new Ingot_JQuery_JqGrid_Column('author'));
		 $grid->addColumn(new Ingot_JQuery_JqGrid_Column('purchased'));

		 $grid->registerPlugin(new Ingot_JQuery_JqGrid_Plugin_ToolbarFilter());
		 $this->view->grid = $grid->render();
    }
}

Read the rest of this entry »

Written by Andy Roberts

April 5th, 2010 at 7:02 pm

Posted in PHP,Zend Framework

Tagged with , ,

Object Oriented Programming in PHP

2 comments

OOP (Object Oriented Programming) is a programming paradigm, which uses objects consisting of properties and methods that interact together to design applications.

You can achieve many benefits using OOP :

  • Maintainability: software objects model real world objects by encapsulating data and methods.  This makes understanding the code structure and locating errors easier.
  • Simplicity: reduces complexity by increasing cohesion (grouping things together which belong together) and reducing coupling (interconnections).
  • Reusability: objects can be reused in different applications.

However, until only a few years ago PHP lacked strong object orientation, this led to much criticism as developers just coded, often procedurally with no separation between presentation and business logic. The result was badly architected and unmaintainable code.

Programming in an object-oriented manner is not hard, its just simply choosing a different way of thinking.

Read the rest of this entry »

Written by Andy Roberts

April 3rd, 2010 at 6:48 pm

Posted in PHP

Tagged with ,

The software it no worky!

leave a comment

For the times when you have to explain some random unexpected behavior to a user,

Dilbert.com

Dilbert.com

Written by Andy Roberts

March 31st, 2010 at 6:01 pm

Posted in Personal

Design Patterns: Traversing the Tree

2 comments

The understanding of good programming principles is essential for any developer but sometimes the implementations, and when to use them can be hard to grasp. This post will demonstrate the benefits of design patterns using the adjacency list model which represents a tree structure through a column referring to it’s parent node.

We can create an object-oriented representation of an adjacency list using the composite pattern to “compose” objects into tree structures, along with the visitor pattern to allow a visitor object to “visit” each element in an hierarchy to perform some operation on that node.


$list = array(
 array('id' => 1,     'name' => 'Design Patterns',   'parent' => 0),
 array('id' => 2,     'name' => 'Creational',        'parent' => 1),
 array('id' => 3,     'name' => 'Structural',        'parent' => 1),
 array('id' => 4,     'name' => 'Behaviour',         'parent' => 1),
 array('id' => 5,     'name' => 'Abstract Factory',  'parent' => 2),
 array('id' => 6,     'name' => 'Factory Method',    'parent' => 2),
 array('id' => 7,     'name' => 'Prototype',         'parent' => 2),
 array('id' => 8,     'name' => 'Singleton',         'parent' => 2),
 array('id' => 9,     'name' => 'Adapter',           'parent' => 3),
 array('id' => 10,    'name' => 'Composite',         'parent' => 3),
 array('id' => 11,    'name' => 'Decorator',         'parent' => 3),
 array('id' => 12,    'name' => 'Facade',            'parent' => 3),
 array('id' => 13,    'name' => 'Proxy',             'parent' => 3),
 array('id' => 14,    'name' => 'Command',           'parent' => 4),
 array('id' => 15,    'name' => 'Iterator',          'parent' => 4),
 array('id' => 16,    'name' => 'Strategy',          'parent' => 4),
 array('id' => 17,    'name' => 'Observer',          'parent' => 4),
 array('id' => 18,    'name' => 'Visitor',           'parent' => 4),
 array('id' => 19,    'name' => 'Implemented',       'parent' => 9),
 array('id' => 20,    'name' => 'Implemented',       'parent' => 10),
 array('id' => 21,    'name' => 'Implemented',       'parent' => 15),
 array('id' => 22,    'name' => 'Implemented',       'parent' => 18),
 );
$tree = new Tree_Adapter_AdjacencyList($list);

echo $tree->fetchChildren(1)
          ->accept(new Tree_Visitor_Html());

The output will contain a pretty indented XHTML unordered list, which lists the children of the root node Design Patterns, each node has been formatted by the Tree_Visitor_Html.

Implementation

You can browse the source code repository below, covering the composite, visitor, adaptor and iterator patterns with a simple example based on an adjacency list.

Browse Source Code

Download Tree.zip – 7kb

Read the rest of this entry »

Written by Andy Roberts

March 29th, 2010 at 6:00 pm

Posted in Design Patterns,PHP

Redirect to Subdomain

4 comments

The two methods which instantly popped into my head, used PHP native function header to send raw HTTP headers or the powerful mod_rewrite within Apache, which uses rule-based rewriting to rewrite requested URLs on the fly.

For example, I needed to redirect all users hitting iphp.co.uk, to my subdomain blog.iphp.co.uk.

Read the rest of this entry »

Written by Andy Roberts

March 22nd, 2010 at 12:46 am

Posted in Apache

Tagged with ,

Anonymous Functions

leave a comment

Apart from the squealing of joy when upgrading to PHP 5.3, the second thing I did was write my first anonymous function, otherwise affectionately known as lambda functions and closures.

Lambda Function

A lambda function is an anonymous PHP function that can be stored in a variable and passed as an argument to other functions or methods.


<?php
$lambda = function() { print "Hello World"; };
$lambda();

Put simply we have throw-away functions, which are especially useful with a wide array of PHP functions that accept a callback. In the past, using callbacks usually would result in a sinking feeling as your code was eroded away with numerous private functions.

However, if you were cunning and threw some ninja PHP-fu moves, you could of used create_function to have a poor man’s lambda function with some less ugly drawbacks, anyway kudos to you if so.

The most obvious use for lambda functions is in conjunction with native PHP functions like array_maparray_walk and array_filter.

The following lambda example filters the odd numbers from a range of numbers.

<?php
$output = array_filter(range(1, 10), function ($number) { return $number % 2; });

or alternatively, lambda function passed as a variable,

<?php
$oddNumber = function ($number) { return $number % 2; };
$output = array_filter(range(1, 10), $oddNumber);

Closure

A closure is a lambda function that is aware of its surrounding context.

Closures are just like lambda functions, but smarter in the sense that they have the ability to interact with variables from outside their environment.

Looking at the example above, how would we check for even and odd numbers? You can either create another lambda function or use a closure.

<?php
$parity = function($type) {
    return function ($number) use ($type) {
        return ($type == 'even') ? $number % 2 == false : $number % 2 == true; };
};

$output = array_filter(range(1, 10), $parity('even'));

Now, the $parity function accepts a parameter called $type, to indicate whether to filter odd or even numbers.

Even for such a mundane example, this does open lots of great possibilities and simplifies your code when used appropriately.

Written by Andy Roberts

March 21st, 2010 at 6:57 pm

Posted in PHP

Tagged with

Calculating Working Days in PHP

2 comments

The issue of calculating working days is a common occurrence whenever dealing with business systems, for example, human resource applications for calculating employee’s holiday entitlements, or in an variety of reports which deal with dates, or key performance indicators.

Implementation

A simple and efficient algorithm is used for calculating working days, by taking the number of complete weeks and remaining days within any given period. Each complete week is multiplied by the number of working days, and the remaining days enumerated.

Public and bank holidays are supported, currently only for England and Wales, which incorporates all the eight permanent bank holidays, with the addition of determining substitute days for holidays which fall on weekends.

The gauss algorithm is used for Easter Sunday and floating holiday algorithm for calculating first and last Monday for the May and August bank holidays.

Download

WorkDay and Holiday_EnglandWales classes, including PHPunit tests.

Download .zip – 5kb

Examples

Working days for a single month

$workDay = new WorkDay(new Holiday_EnglandWales());
echo $workDay->count('2010-01-01', '2010-01-31'); // 20 working days

Working days for a yearly range

$workDay = new WorkDay(new Holiday_EnglandWales());
echo $workDay->count('2010-01-01', '2011-01-01'); // 253 working days

Working days for a bi-yearly range with a single holiday event

$holiday = new Holiday_EnglandWales();
$holiday->addHoliday('Queens Diamond Jubilee', strtotime('2012-06-05'));

$workDay = new WorkDay($holiday);
echo $workDay->count('2010-01-01', '2012-12-31'); // 756 working days

Public and bank holidays within a particular year

$holiday = new Holiday_EnglandWales();
$holiday->getHoliday('2010');

Written by Andy Roberts

March 14th, 2010 at 8:25 pm

Posted in PHP

Tagged with , ,

Zend Server 5.0 Review

3 comments

At work,  we all know and love PHP, and regard Zend as a good business partner. We have purchased Zend Studio and use Zend Framework everyday to build and improve our internal logistics systems.

Our development team is a small band of 3 experienced developers, whom have solely developed and maintain a medium-sized PHP project with 2 production servers supporting over 80 users . We always look forward to future PHP versions, so it was a pleasant surprise to see the Zend Server, a complete, enterprise-ready LAMP stack being offered by Zend.

Zend Server is available in two different editions, Zend Server and Zend Server Community Editon (CE) both provide different levels of support for critical and non-critical business applications.

Zend Server Community Edition (CE)

Zend Server Community Edition (CE) is a trimmed down version of Zend Server that is absolutely free to use in development or in production. It is simple to install and to use, provides basic performance optimization, and is supported only through the Zend support forums.


Each edition provides  a web-based interface for configuring your server and works with your existing package management system to obtain the latest stable version of PHP and a number of supported extensions. This helps to manage the dependencies that are required for PHP and save time on new installs or administering upgrades.

They come bundled with a number of commonly used components :

  • Zend certified version of PHP 5.2.x or 5.3.x
  • Apache 2.2.x Web server
  • MySQL 5.x database
  • Web-based PHP administrator console
  • Zend Framework
  • Zend Extensions including Optimiser, Data Cache, Debugger, Java Bridge and Guard Loader

Read the rest of this entry »

Written by Andy Roberts

March 8th, 2010 at 12:12 am

Posted in PHP

Tagged with ,