Sunday, October 7, 2012

The teachings of Warren Buffet

What Warren Buffet says about basic investing, spending, savings are so true. Most of us know it, however too many of us do not live it.
If it does make a change in your life, thank HIM (I mean God) because this is common sense.

1. On Earning:
Do not depend on a single income. Invest and create a second/ third source of income:
This means when you are young your first task should be saving and investing. By creating a second source of income you are quickly reducing your dependence on your job. This could help you to set out on your own one day. The quicker you can do it, the better.

2. On Spending:
If you buy things that you do not need, you may soon have to sell things you need: It kind of summarizes Gen X’s reaction towards ‘luxuries’. As a part of Gen X we were perhaps criticised for some of our expenses, so it could be a generational thing even for WB. However, having goals and knowing where you are going, and not spending just to ‘show off’ are important lessons for all generations.

3. On Savings:
Do not spend what is left after spending, instead spend after you save/invest:
Also called ‘Pay Yourself First’. If you realise that investing in a pension plan or for your kid’s education is just helping you to save more later on. It is not a sacrifice, it is just postponing consumption. So understand, invest and then spend.

4. On taking Risk:
Never test the depth of the river with both your feet:
If you are doing something, do small. If you are a first gen investor, do not be carried away by equity lovers like me and put all your money in equity. Do a SIP with a small amount, and test the waters. Do a SIP of Rs. X (which could be 10% of your take home pay) for 5 years and then step up. And for heavens sake understand risk of inflation, and the concept of real returns

5. On Investing: 
Do not put all eggs in one basket:
Immaterial of who you are and how much you understand, create a portfolio. A full range lunch plate is always better than just one item. So create a portfolio with bonds, bond funds, PPF, NSC, equity, mutual funds, and on the risk side medical and term insurance.

6. On Expectation:
Honesty is expensive, do not expect it from cheap people:
Not everybody is honest, nor does everybody want to be honest. Honest advisers are difficult to find especially in Health and Wealth, be careful.

Thursday, April 28, 2011

::JUDGE YOURSELF::

::JUDGE YOURSELF::

Once upon a time there was a painter who had just completed his course. He took 3 days and painted beautiful scenery. He wanted people's opinion about his caliber and painting skills.

He put his creation at a busy street-crossing. And just down below aboard which read -"I have painted this piece. Since I'm new to this profession I might have committed some mistakes in my strokes etc. Please put a cross wherever you see a mistake."

While he came back in the evening to collect his painting he was completely shattered to see that whole canvass was filled with Xs
(crosses) and some people had even written their comments on the painting.

Disheartened and broken completely he ran to his master's place and burst into tears.
This young artist was breathing heavily and master heard him saying"I'm useless and if this is what I have learnt to paint I'm not worth becoming a painter. People have rejected me completely. I feel like dying"

Master smiled and suggested "My Son, I will prove that you are a great
artist and have learnt flawless painting. Do as I say without questioning it. It WILL work."

Young artist reluctantly agreed and two days later early morning he presented a replica of his earlier painting to his master. Master took that gracefully and smiled.

"Come with me. " master said.

They reached the same street-square early morning and displayed the same painting exactly at the same place. Now master took out another board which read -

"Gentlemen, I have painted this piece. Since I'm new to this profession I might have committed some mistakes in my strokesetc. I have put a box with colors and brushes just below. Please do a favor. If you see a mistake, kindly pick up the brush and correct it."

Master and disciple walked back home.

They both visited the place same evening. Young painter was surprised to see that actually there was not a single correction done so far. Next day again they visited and found painting remained untouched. They say the painting was kept there for a month for no correction came in.!

Moral of the Story:
It is easier to criticize, but DIFFICULT TO IMPROVE.! Don't get carried away or judged yourself by someone's criticism and feel depressed. Judge Yourself.! You are your best judge....

Sunday, November 1, 2009

Thursday, July 9, 2009

Image crop component for CakePHP

\app\controllers\components\images.php

<?php
/**
* Images component for CakePHP.
* @author Amit Chavda <amit.chavda@gmail.com>
* @copyright Amit Chavda
* @link http://www.cakephp.org CakePHP
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

class ImagesComponent extends Object {
/**
* The Upload Directory
* @access public
* @var string
*/
var $upload_dir = "";

/**
* The Upload Directory Path
* @access public
* @var string
*/
var $upload_path = "";

/**
* New name of the large image & New name of the thumbnail image
* @access public
* @var string
*/
var $large_image_name = "resized_pic.jpg";
var $thumb_image_name = "thumbnail_pic.jpg";

/**
* The Max File Size. Approx 1MB
* @access public
* @var string
*/
var $max_file = "1148576";

/**
* The Max width allowed for the large image
* @access public
* @var string
*/
var $max_width = "500";

/**
* The Width of thumbnail image
* @access public
* @var string
*/
var $thumb_width = "100";

/**
* The Height of thumbnail image
* @access public
* @var string
*/
var $thumb_height = "100";

//Image functions
//You do not need to alter these functions
function resizeImage($image,$width,$height,$scale) {
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
$source = imagecreatefromjpeg($image);
imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
imagejpeg($newImage,$image,90);
chmod($image, 0777);
return $image;
}

//You do not need to alter these functions
function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
$source = imagecreatefromjpeg($image);
imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
imagejpeg($newImage,$thumb_image_name,90);
chmod($thumb_image_name, 0777);
return $thumb_image_name;
}

//You do not need to alter these functions
function getHeight($image) {
$sizes = getimagesize($image);
$height = $sizes[1];
return $height;
}

//You do not need to alter these functions
function getWidth($image) {
$sizes = getimagesize($image);
$width = $sizes[0];
return $width;
}
}
?>



\app\controllers\images_controller.php

<?php
class ImagesController extends AppController {

var $components = array('Images');
var $helpers = array('Html', 'Form', 'Ajax');

function index() {
$error = '';
$this->Images->upload_dir = WWW_ROOT . 'files' . DS . 'upload_pic' . DS;
//Image Locations
$large_image_location = $this->Images->upload_dir.$this->Images->large_image_name;
$thumb_image_location = $this->Images->upload_dir.$this->Images->thumb_image_name;

//Create the upload directory with the right permissions if it doesn't exist
if(!is_dir($this->Images->upload_dir)){
mkdir($this->Images->upload_dir, 0777);
chmod($this->Images->upload_dir, 0777);
}

//Check to see if any images with the same names already exist
if (file_exists($large_image_location)){
if(file_exists($thumb_image_location)){
$thumb_photo_exists = 'files/upload_pic/'.$this->Images->thumb_image_name;
}else{
$thumb_photo_exists = "";
}
$large_photo_exists = 'files/upload_pic/'. $this->Images->large_image_name;
$this->set('current_large_image_width', $this->Images->getWidth($large_image_location));
$this->set('current_large_image_height', $this->Images->getHeight($large_image_location));
} else {
$thumb_photo_exists = '';
$large_photo_exists = '';
}
$this->set('large_photo_exists', $large_photo_exists);
$this->set('thumb_photo_exists', $thumb_photo_exists);
$this->set('thumb_width', $this->Images->thumb_width);
$this->set('thumb_height', $this->Images->thumb_height);

if(!empty($this->data)) {

if(isset($this->data['Image']['image'])) {
//Get the file information
$userfile_name = $this->data['Image']['image']['name'];
$userfile_tmp = $this->data['Image']['image']['tmp_name'];
$userfile_size = $this->data['Image']['image']['size'];
$filename = basename($this->data['Image']['image']['name']);
$file_ext = substr($filename, strrpos($filename, '.') + 1);

//Only process if the file is a JPG and below the allowed limit
if((!empty($this->data['Image']["image"])) && ($this->data['Image']['image']['error'] == 0)) {
if (($file_ext!="jpg") && ($userfile_size > $this->Images->max_file)) {
$error= "ONLY jpeg images under 1MB are accepted for upload";
}
}else{
$error= "Select a jpeg image for upload";
}
//Everything is ok, so we can upload the image.
if (strlen($error)==0){
if (isset($this->data['Image']['image']['name'])){

move_uploaded_file($userfile_tmp, $large_image_location);
chmod($large_image_location, 0777);

$width = $this->Images->getWidth($large_image_location);
$height = $this->Images->getHeight($large_image_location);
//Scale the image if it is greater than the width set above
if ($width > $this->Images->max_width){
$scale = $this->Images->max_width/$width;
$uploaded = $this->Images->resizeImage($large_image_location,$width,$height,$scale);
}else{
$scale = 1;
$uploaded = $this->Images->resizeImage($large_image_location,$width,$height,$scale);
}
//Delete the thumbnail file so the user can create a new one
if (file_exists($thumb_image_location)) {
unlink($thumb_image_location);
}
}
//Refresh the page to show the new uploaded image
$this->redirect(array('controller' => "images", 'action' => 'index'));
}
}

if(isset($this->data['Image']['x1'])) {
//Get the new coordinates to crop the image.
$x1 = $this->data['Image']["x1"];
$y1 = $this->data['Image']["y1"];
$x2 = $this->data['Image']["x2"];
$y2 = $this->data['Image']["y2"];
$w = $this->data['Image']["w"];
$h = $this->data['Image']["h"];
//Scale the image to the thumb_width set above
$scale = $this->Images->thumb_width/$w;
$cropped = $this->Images->resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
$this->redirect(array('controller' => "images", 'action' => 'index'));
}
$this->set('error', $error);
}
}

function deleteImage() {
$this->Images->upload_dir = WWW_ROOT . 'files' . DS . 'upload_pic' . DS;
//$large_image_location = $this->Images->upload_dir.$this->Images->large_image_name;
$thumb_image_location = $this->Images->upload_dir.$this->Images->thumb_image_name;

/*if (file_exists($large_image_location)) {
unlink($large_image_location);
}*/
if (file_exists($thumb_image_location)) {
unlink($thumb_image_location);
}
$this->redirect(array('controller' => "images", 'action' => 'index'));
}
}
?>


\app\models\image.php

<?php
class Image extends AppModel {
var $name = 'Image';
var $useTable = false;
}
?>

\app\views\images\index.ctp


<?php
echo $javascript->link('jquery-pack');
echo $javascript->link('jquery.imgareaselect-0.3.min');
?>
<?php
//Only display the javacript if an image has been uploaded
if(strlen($large_photo_exists)>0){
//$current_large_image_width = getWidth($large_image_location);
//$current_large_image_height = getHeight($large_image_location);?>
<script type="text/javascript">
function preview(img, selection) {
var scaleX = <?php echo $thumb_width;?> / selection.width;
var scaleY = <?php echo $thumb_height;?> / selection.height;

$('#thumbnail + div > img').css({
width: Math.round(scaleX * <?php echo $current_large_image_width;?>) + 'px',
height: Math.round(scaleY * <?php echo $current_large_image_height;?>) + 'px',
marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
});
$('#x1').val(selection.x1);
$('#y1').val(selection.y1);
$('#x2').val(selection.x2);
$('#y2').val(selection.y2);
$('#w').val(selection.width);
$('#h').val(selection.height);
}

$(document).ready(function () {
$('#save_thumb').click(function() {
var x1 = $('#x1').val();
var y1 = $('#y1').val();
var x2 = $('#x2').val();
var y2 = $('#y2').val();
var w = $('#w').val();
var h = $('#h').val();
if(x1=="" || y1=="" || x2=="" || y2=="" || w=="" || h==""){
alert("You must make a selection first");
return false;
}else{
return true;
}
});
});

$(window).load(function () {
$('#thumbnail').imgAreaSelect({ aspectRatio: '1:1', onSelectChange: preview });
});

function checkform(){
document.getElementById('ImageX1').value = document.getElementById('x1').value;
document.getElementById('ImageY1').value = document.getElementById('y1').value;
document.getElementById('ImageX2').value = document.getElementById('x2').value;
document.getElementById('ImageY2').value = document.getElementById('y2').value;
document.getElementById('ImageW').value = document.getElementById('w').value;
document.getElementById('ImageH').value = document.getElementById('h').value;
//alert(document.getElementById('x1').value);
return true;
}

</script>
<?php }?>
<h1>Photo Upload and Crop</h1>
<?php
//Display error message if there are any
if(isset($error) and strlen($error)>0){
echo "<ul><li><strong>Error!</strong></li><li>".$error."</li></ul>";
}
if(strlen($large_photo_exists)>0 && strlen($thumb_photo_exists)>0){
echo "<p><strong>NOTE:</strong> If the thumbnail image looks the same as the previous one, just hit refresh a couple of times.</p>";
echo '<img src="'.$large_photo_exists.'" style="float: left; margin-right: 10px;" id="thumbnail" alt="Main" /> <img src="'.$thumb_photo_exists.'" style="float: left; margin-right: 10px;" id="thumbnail" alt="Thumbnail" />';
echo "<p>".$html->link('Delete Images', array('controller'=>'images', 'action'=>'deleteImage'))."</p>";
}else{
if(strlen($large_photo_exists)>0){?>
<h2>Create Thumbnail</h2>
<div align="center">
<img src="<?php echo $large_photo_exists;?>" style="float: left; margin-right: 10px;" id="thumbnail" alt="Create Thumbnail" />
<div style="float:left; position:relative; overflow:hidden; width:<?php echo $thumb_width;?>px; height:<?php echo $thumb_height;?>px;">
<img src="<?php echo $large_photo_exists;?>" style="position: relative;" alt="Thumbnail Preview" />
</div>
<br style="clear:both;"/>
<?php echo $form->create('Image', array('action' => 'index', 'name'=>'thumbnail', 'onsubmit'=>'return checkform()'))?>
<?php echo $form->hidden('x1');?>
<?php echo $form->hidden('y1');?>
<?php echo $form->hidden('x2');?>
<?php echo $form->hidden('y2');?>
<?php echo $form->hidden('w');?>
<?php echo $form->hidden('h');?>

<input type="hidden" name="x1" value="" id="x1" />
<input type="hidden" name="y1" value="" id="y1" />
<input type="hidden" name="x2" value="" id="x2" />
<input type="hidden" name="y2" value="" id="y2" />
<input type="hidden" name="w" value="" id="w" />
<input type="hidden" name="h" value="" id="h" />
<!--<input type="submit" name="upload_thumbnail" value="Save Thumbnail" id="save_thumb" />-->
<?php echo $form->submit('Save Thumbnail', array('div' => false));?>
<?php echo $form->end(); ?>
</div>
<hr />
<?php } ?>
<h2>Upload Photo</h2>
<?php echo $form->create('Image', array('action' => 'index', 'name'=>'photo', 'type'=>'file'))?>
Photo <?php echo $form->file('image');?> <?php echo $form->submit('Upload', array('div' => false));?>
<?php echo $form->end(); ?>
<?php } ?>

All The Best,
Amit Chavda

Monday, February 2, 2009

Blog with Zend Framework

In the typical blog application, we must have below points.

  1. Authentication for Authors
  2. Authorization to create/edit/delete/read entries
  3. Methods for adding new entries, and modifying them
  4. Publishing entries as RSS and Atom
  5. Permalinks unique for each entry (SEO friendly of course)
  6. Commenting system
  7. Spam detection for new comments
  8. Perhaps, trackback detection

Below tools will be required for this blog application
  1. Zend Framework 1.5
  2. PHPUnit
  3. HTMLPurifier
  4. Blueprint CSS Framework
  5. jQuery

The Model-View-Controller

The Model-View-Controller Pattern (or MVC as it's usually abbreviated) is a general solution to the question of how to separate responsibilities in an application in a highly structured manner. The pattern's name mentions all three separations: Model, View and Controller. Although MVC may seem to be one of those esoteric concepts in programming, it's actually quite a simple concept. You pick some nugget of functionality, determine it's purpose, and assign to one of the three separations, and then to a specific class. Once everything is split correctly, you end up with small pieces which are reusable, centralised, accessible, and fit together into building blocks with an abstract API - working now with abstracted APIs makes incremental changes extremely simple. With everything tidily organised into objects with specific responsibilities the cost of change is normally reduced significantly.

To be clear, the MVC is common as dirt. It is widely used in the Zend Framework, Solar, Symfony, Ruby on Rails, merb, Django, Spring, and countless other frameworks. It is an unescapable concept when adopting a framework for web applications in almost any language.

The Model

The Model is responsible for maintaining state between HTTP requests in a PHP web application. Any data which must be preserved between HTTP requests is destined for the Model segment of your application. This goes for user session data as much as rows in an external database. It also incorporates the rules and restraints governing that data which is referred to as the "business logic".

The View

The View is responsible for generating a user interface for your application. In PHP, this is often narrowly defined as where to put all your presentational HTML. While this is true, it's also the place where you can create a system of dynamically generating HTML, RSS, XML, JSON or indeed anything at all being sent to the client browser or application.

The View is ordinarily organised into template files but it can also simply be echoed from or manipulated by the Controller prior to output. It's essential to remember that the View is not just a file format - it also encompasses any PHP code or parsed tags used to organise, filter, decorate and manipulate the format based on data retrieved from one or more Models (or as is often the case, passed from the Model to the View by the Controller).

The Controller

Controllers are almost deceptively simple in comparison. The primary function of the Controller is to control and delegate. In a typical PHP request to an MVC architecture, the Controller will retrieve user input, supervise the filtering, validation and processing of that input, manage the Model, and finally delegate output generation to the View (optionally passing it one or more Models required to process the current template). The Controller also has a unique difference from other forms of PHP architectural forms since it only requires a single point of entry into the application - almost inevitably index.php.

Controller vs Model

No quick tour of MVC would be complete without a brief mention of at least one extremely common variance in MVC apps. Borrowing a term, it's the idea of a Fat Model and Thin Controller.

A Fat Model, is a Model which takes on as much business logic and data manipulation as you can fit into it. The result is a large body of reusable logic accessible from any Controller. This, in theory, results in a Thin Controller - when all this logic is bundled into the Model behind some suitable APIs, the Controller's average size should be reduced. Less Controller code, less obscuring crud hiding exactly what a Controller is doing.

The opposite is a Thin Model/Fat Controller - business logic is dumped into the Controller which obviously increases its size, and secondly means that code is not reusable (unless you decide to reuse the Controller from another Controller - which is rarely a good idea efficiency wise).

In concert these three segments of an application implement a Model-View-Controller architecture. It's become a widely recognised solution suitable for web applications and it's evident in the majority of the current generation of framework for many programming languages.

In the Zend Framework, these three separations are represented by the Zend_Db, Zend_View and Zend_Controller components. You'll be hearing a lot about these three and the classes they are composed of in the chapters to come! Together these form the backbone of the Zend Framework's MVC architecture and underpin a lot of it's best practices.

The Model-View-Controller was originally used to promote the "separation of concerns" in desktop GUI applications. By separating each concern into an independent layer of the application, it led to a decrease in coupling which in turn made applications easier to design, write, test and maintain. Although GUI applications have turned away from the MVC in recent years, it's proven to be highly effective when applied to web applications.

In the framework this adds a lot of predictable structure since each segment of MVC for any one supported request is segregated into its own group of files. The Controller is represented by Zend_Controller_Front and Zend_Controller_Action subclasses, the Model by subclasses of Zend_Db_Table, and the View by .phtml template files and View Helpers. The Zend Framework manages how each is orchestrated in the big picture, leaving you free to focus on just those groupings without worrying about all the code combining them together.

In a sense it's like building a house where the foundations, walls and internal wiring and plumbing are already in place, and all that's left is the internal decoration and a roof. It may take some time to learn how to decorate and roof the prepared sections but once you have learned how, later houses are finished a lot faster!

Set Up the Project Structure

Let's first create a basic MVC project structure for our application under the directory QuickStart:

zfblog/
---application/
------controllers/
------models/
------views/
---------scripts/
---library/
---public/
The "application" directory is where we place all the components of an application implementing the Model-View-Controller. Inside "application", "controllers", "models" and "views" represent respective locations of controller, model and view source code and templates.

Download & Install ZF

Download the latest version of Zend Framework and extract the contents of the library folder to your project's library directory. You should now find the top-level Zend directory which contains all Zend Framework components under your library directory.

That's it! Zend Framework is now installed and ready to use.

At the end of this step you should have the directory structure in place containing some empty files as follows (create them now, and we'll fill them in later):

/application/Bootstrap.php
/application/controller/IndexController.php
/application/views/scripts/index/index.phtml
/public/index.php
/public/.htaccess

Create a Rewrite Rule

Zend Framework's MVC implementation makes use of the Front Controller pattern. You must therefore rewrite all incoming requests (except those for static resources, which your application need not handle) to a single script that will initialize the FrontController and route the request.
If you're using mod_rewrite for the Apache web server, create the file QuickStart/public/.htaccess with the following contents:

# public/.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* index.php

This set of rewrite rules specify that if the file exists under the document root directory, it should simply be served as a static resource. Otherwise, the request is for dynamic content and should be rewritten to our index.php script. Since all requests for non-static content will be rewritten to it, the index.php script serves as the entry point to our application.

Create a Bootstrap File

A Bootstrap.php file exists in the "application" directory which represents a class called "Bootstrap". It's purpose is to initialise the Zend Framework, adjust environment settings (for example, timezone and error_reporting level), and otherwise make application specific tweaks and additions before a HTTP request is processed. Most tutorials take an alternative view and put Bootstrap code into index.php. I strongly suggest you avoid this and use an actual class to organise the Bootstrap code - it makes it a lot easier to read if nothing else!


<?
require_once 'Zend/Loader.php';

class Bootstrap
{

public static $frontController = null;

public static function run()
{
self::setupEnvironment();
self::prepare();
$response = self::$frontController->dispatch();
self::sendResponse($response);
}

public static function setupEnvironment()
{
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
date_default_timezone_set('Europe/London');
}

public static function prepare()
{
self::setupFrontController();
self::setupView();
}

public static function setupFrontController()
{
Zend_Loader::registerAutoload();
self::$frontController = Zend_Controller_Front::getInstance();
self::$frontController->throwExceptions(true);
self::$frontController->returnResponse(true);
self::$frontController->setControllerDirectory(
dirname(__FILE__) . '/controllers'
);
}

public static function setupView()
{
$view = new Zend_View;
$view->setEncoding('UTF-8');
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
}

public static function sendResponse(Zend_Controller_Response_Http $response)
{
$response->setHeader('Content-Type', 'text/html; charset=UTF-8', true);
$response->sendResponse();
}

}

?>
The Bootstrap class has two main methods here: run() and prepare(). run() executes a full application request, whereas prepare() only sets up (but doesn't execute) the Front Controller. For now let's focus on run() and all the steps our Bootstrap takes when it's called. The prepare() method is something we'll use when applying TDD or BDD to developing controllers, since we then delegate controller execution to the testing framework.

The first thing is to setup the local environment. So here we've set a timezone (as required by PHP5), enabled PHP's display_errors option, and set a fairly stiff error reporting level.

Secondly we prepare the Front Controller. Zend_Controller_Front is an implementation of the Front Controller Design Pattern, which acts as a single point of entry into a Model-View-Controller architected application.

Here, the prepare() stage involves setting up the Front Controller:
  • get an instance of Zend_Controller_Front
  • set it to throw all Exceptions (might want to disable this in production)
  • set it to return a Response object when dispatched
  • tell it where to find the default controllers
The last step of prepare() makes some changes to the View. The reason of this is to show how to make an extremely common change - adding support for UTF-8 encoded output (such as my name!).

/public/index.php

<?
// Update after deployment for location of non-public files
$root = dirname(dirname(__FILE__));

// We're assuming the Zend Framework is already on the include_path
set_include_path(
$root . '/application' . PATH_SEPARATOR
. $root . '/library' . PATH_SEPARATOR
. get_include_path()
);

require_once 'Bootstrap.php';

Bootstrap::run();

?>

Implementing The Index Controller

Before we continue, a quick word on controller/view wiring. Some time back it was decided to make automated rendering the default mode of operation for the Zend Framework. This means Controller will rarely need manual intervention to render a View - instead an Action Helper called the ViewRenderer (which should sound familiar since it used it to add UTF-8 encoding in our Bootstrap!) is called upon. This helper locates a View matching the name of the current Controller and Action and automatically renders it. There are ways to disable this automated rendering as documented in the manual - which is often useful when you want to bypass Zend_View (e.g. maybe you're outputting finalised JSON or XML and no further templating or processing is needed).

If a URL to a Zend Framework application does not contain a controller or action reference, then the Zend Framework uses the default "index" value. Since we only intend requesting to the root URL of "http://localhost/zfblog/public/", we'll need an Index Controller containing an Index Action. Let's create one!

Add a file called "IndexController.php" in application/controllers containing:

<?
class IndexController extends Zend_Controller_Action
{

public function indexAction()
{
$this->view->title = 'Hello, World!';
}

}

?>

All controllers must extend Zend_Controller_Action, since it contains all later referenced internal methods and properties common to all Controllers. If you need to tweak the basic Controller to add new methods or properties you may do so using a subclass of Zend_Controller_Action and then making all application Controllers extend this subclass instead. Be very careful however in subclassing this way - it's useful for adding new properties to the class, but additional methods are often better added as discrete Action Helpers.

Above we've told the View that templates might refer to a variable "title" and given it a value of "Hello, World!". Once the Action method is done, the ViewRenderer will kick in and try to render a template located at "/path/to/application/views/scripts/index/index.phtml".


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<title><?php echo $this->escape($this->title) ?></title>
</head>
<body>
<p>Hello, World!</p>
</body>
</html>

Did it work?

Go ahead and open up your browser. The base url of http://localhost/zfblog/public/ should now do it's thing and return a HTML document containing "Hello, World!".

Adding Blueprint to the Project

You can download the Blueprint CSS framework from http://code.google.com/p/blueprintcss/. It is released under a dual licensing system using a Modified MIT License, or the GNU General Public License. Decompress the archive of the latest 0.7 release (as at time of writing) and return to our current project's public directory.

Inside public create a new directory called css. Into this new directory, copy the decompressed blueprint directory and LICENSE file from the Blueprint distribution. We retain the license so future redistributions of the project (if any) include a copy for reference.

Blueprint contains a default set of reasonable styling. This include the most fantastic of them all - when included correctly into any HTML document it sets all browsers to the same set of defaults. This completely negates the usual pain of Firefox, IE6, IE7 and Safari having sufficiently different styles internally that designs need a ton of small browser specific adjustments by hand. By hand is evil - Blueprint does it without interference.

To complete this step, create an empty CSS file called style.css inside the /public/css directory. We'll include this in our HTML as a point for customising the Blueprint CSS and adding additional styling as needed.

Creating a Test HTML Document applying Blueprint CSS

open up the /application/views/scripts/index/index.phtml template used by our default Index controller and edit to contain the following.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<title><?php echo $this->escape($this->title) ?></title>
<link rel="stylesheet" href="css/blueprint/screen.css" type="text/css" media="screen, projection">
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen, projection">
<link rel="stylesheet" href="css/blueprint/print.css" type="text/css" media="print">
<!--[if IE]><link rel="stylesheet" href="css/blueprint/ie.css" type="text/css" media="screen, projection"><![endif]-->
</head>
<body>

<div class="container">

<div class="block">
<div id="zfHeader" class="column span-24">
<h1>Amit Chavda</h1>
</div>
</div>

<div class="block">
<div id="zfContent" class="column span-16">
<p>Welcome To My Blog</p>
</div>

<div id="zfExtraRight" class="column span-7">
<p>Welcome To My Blog</p>
</div>
</div>

<div class="block">
<div id="zfFooter" class="span-24">
<p>Copyright &copy; 2008 Pádraic Brady</p>
</div>
</div>

</div>

</body>
</html>

Splitting persistent markup into a Zend_Layout template

Create a new file in /application/views/layouts called common.phtml. What we'll do is copy in the above index.phtml template but delete the sections that are likely to change with different page views - namely the entries.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<title><?php echo $this->escape($this->title) ?></title>
<link rel="stylesheet" href="css/blueprint/screen.css" type="text/css" media="screen, projection">
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen, projection">
<link rel="stylesheet" href="css/blueprint/print.css" type="text/css" media="print">
<!--[if IE]><link rel="stylesheet" href="css/blueprint/ie.css" type="text/css" media="screen, projection"><![endif]-->
</head>
<body>

<div class="container">

<div class="block">
<div id="zfHeader" class="column span-24">
<h1>Lorem Ipsum</h1>
</div>
</div>

<div class="block">
<div id="zfContent" class="column span-16">

<?php echo $this->layout()->content ?>

</div>

</div>

<div class="block">
<div id="zfFooter" class="span-24">
<p>Copyright &copy; 2008 Pádraic Brady</p>
</div>
</div>

</div>

</body>
</html>
With our Layout template prepared, let's remove all those common items from our Action specific template at /application/views/scripts/index/index.phtml:

<h2>One</h2>
<p><?php echo gmdate('D, j M Y H:i:s T', time()) ?><br />
Posted by Pádraic Brady</p>
<p>Content for One</p>
<hr />
<p>Tags: Lorem, Ipsum, Excepteur</p>

<h2>Two</h2>
<p><?php echo gmdate('D, j M Y H:i:s T', time()) ?><br />
Posted by Pádraic Brady</p>
<p>Content for Tow</p>
<hr />
<p>Tags: Lorem, Ipsum, Excepteur</p>

Setting up Zend_Layout for operation

Getting Zend_Layout working is again quite simple. We just need to make a few additions to our Bootstrap file. Specifically to the setupView() static method.
<?php

require_once 'Zend/Loader.php';

class Bootstrap
{

public static $frontController = null;

public static $root = '';

public static function run()
{
self::setupEnvironment();
self::prepare();
$response = self::$frontController->dispatch();
self::sendResponse($response);
}

public static function setupEnvironment()
{
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', true);
date_default_timezone_set('Europe/London');
self::$root = dirname(dirname(<u>_FILE_</u>));
// FILE constant should have two underscores
// preceeding and following "FILE"
}

public static function prepare()
{
self::setupFrontController();
self::setupView();
}

public static function setupFrontController()
{
Zend_Loader::registerAutoload();
self::$frontController = Zend_Controller_Front::getInstance();
self::$frontController->throwExceptions(true);
self::$frontController->returnResponse(true);
self::$frontController->setControllerDirectory(
self::$root . '/application/controllers'
);
}

public static function setupView()
{
$view = new Zend_View;
$view->setEncoding('UTF-8');
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
Zend_Layout::startMvc(
array(
'layoutPath' => self::$root . '/application/views/layouts',
'layout' => 'common'
)
);
}

public static function sendResponse(Zend_Controller_Response_Http $response)
{
$response->setHeader('Content-Type', 'text/html; charset=UTF-8', true);
$response->sendResponse();
}

}?>

Wednesday, June 18, 2008

Khush Raho

zindagi hai choti, har pal mein khush raho
Office me khush raho, ghar mein khush raho
Aaj paneer nahi hai, daal mein hi khush raho
Aaj gym jane ka samay nahi, do kadam chal ke hi khush raho
Aaj Dosto ka sath nahi, TV dekh ke hi khush raho
Ghar ja nahi sakte to phone kar ke hi khush raho
Aaj koi naraaz hai, uske ish andaz mein bhi khush raho
Jisse dekh nahi sakte uski awaz mein hi khush raho
Jisse paa nahi sakte uske yaado mein hi khush raho
MBA karne ka socha tha, apne job mein he khush raho
Laptop na mila to kya, Desktop mein hi khush raho
bita hua kal jaa chuka hai, ushme meethi yaadein hai, unme hi khush raho
aane wale pal ka pata nahi sapno mein he khush raho
Haste haste ye pal beet jaaenge, aaj mein hi khush raho
Zindagi hai choti, har pal main khush raho.

Lion Part 4

orr yee Aitebaar kuch iss kadar ho jata hai.
har koi begaana orr wo ek apna khaas ho jata hai.
Naa Din ki naa raat ki khabar rehti hai.
wo ajnaabi iss tarah se apna pyaar ho jata hai.

Kisi na kisi ko kisi na kisi pe aitbar ho jata hai,
Ajnabi sa koi shakhs khaas ho jaata hai,
Khubiyon se hi nahi hoti mohabbat sada,
Kabhi- kabhi khamiyon se bhi pyar ho jaata hai
------------------------------------------------------------------
dekho aur bhi bahut hoge tarane aapke pass
kuch apne kuch hamare fasane aapke paas
kya har baar hath jatak ke khade ho jate ho
jara dekho aapna daman khuda ne chhode he kai sitare aapke pass

Ek ada aapke dil churane ki, ek ada aapke dil me bus jane ki,
Chehra apka chand sa aur ek zid hamari chand pane ki.


Wo kahte hain k saari raat aap hume sone nahi dete...
hum kehte hai k apka Noor he aisa hai k
aap hamari raat hone he nahi dete...

Kuch lamhe kuch ehsaas mitaye nahi jate,
Kuch meethe-meethe pal bhulaye nahi jate,
kuch nazre khud hi keh jati hai,
Kuch alfaaz sunaye nahi jate.

us din khuda ne bhi jashn manaya hoga,
Jis din aapko apne hatho se banaya hoga,
Usne bhi bahaye honge aansu,
Jis din aapko yahan bhej khud ko akela paya hoga

Phoolon mein gulab achhaa lagta hai,
Sapno mein khwab achhaa lagta hai,
Aap hamein yaad karti rahe
Hame aapka yeh andaz achaa lagta hai

Kuch nasha to aapki baat ka hai,
kuch nasha to dheemi barsaat ka hai,
humein aap yun hi sharabi na kahiye,
is dil par asar to aap se mulakat ka hai!


Woh inkar karte hain ikrar ke liye,
Nafrat bhi karte hain to pyar ke liye,
Ulti chaal chalte hain yeh ishqwale,
Ankhein band karte hain didar ke liye.


Yaadon ki dhund me aapki parchai si lagti hai,
Kaanon me gunjti shahnai si lagti hai,
Aap karib ho to apnapan hai,
Warna seene me saanse bhi parai si lagti hai.


Woh ruthte rahe hum manate rahe,
Unki raahon me palkein bichate rahe,
Unhone kabhi palat ke bhi na dekha,
Hum ankh jhapkane se bhi katrate rahe.


Teri dosti hum is tarah nibhayenge,
Tum roz khafa hona hum roz manayenge,
Par maan jana manane se,
Warna yeh bheegi palkein le ke kaha jayenge.


Dhoka diya tha jab tum ne mujhe,
Dil se main naraaz tha,
Phir socha ki dil se tumhe nikal dun,
Magar woh kambakth dil bhi tumhare paas tha.....


Koi ankhon se baat kar leta hai,
Koi ankhon mein mulakat kar leta hai,
Bada mushkil hota hai jawab dena,
Jab koi khamosh rehkar sawaal kar leta hai.


Qayamat tak tujhe yaad karenge,
Teri har baat par aitbaar karenge,
Tujhe SMS karne ko to nahi kahenge,
Par phir bhi tere SMS ka intezar karenge.


Kaun jane kab maut ka paigam aa jaye,
Zindagi ki akhari sham aa jaye,
Hum toh dhundhte hain waqt aisa jab,
Hamari zindagi apke kam aa jaye.


Do pal ki bhi khushi na mili to kya
hum umr bhar gam ke sahare ji lenge,
Kya hua jo hamari girlfriend nahi,
hum aapki girlfriend ke sahare ji lenge.


Soch ko badlo, sitare badal jayeng,
Najar ko badlo, nazare badal jayenge,
Kashtiya badalne ki jarurat nahi,
Dishaon ko badlo, KINARE badal jayenge.


Aapki muskan hamari kamjori hai,
Keh na pana hamari majburi hai,
Aap kyon nahin samajhte is khamoshi ko,
Kya khamoshi ko zuban dena jaruri hai?


Hum dua karte hain Khuda se,
ki wo aap jaisa dost aur na banaye,
Ek Cartoon jaisi cheez hai humare paas,
kahin wo bhi common na ho jaye!


Dil mein umeedo ki shamma jala rakhi hai,
Humne apni alag duniya basa rakhi hai,
Is umeed ke saath ki ayega SMS aapka,
Humne mobile par nazrein jama rakhi hein.


Hum na hote to aapko gazal kaun kehta,
apke chehre ko gulab kaun kehta,
Ye to karishma hai hum pyar karne walon ka,
warna pattharon ko Taj Mahal kaun kehta.


Aahat si koi aaye to lagta hai ki tum ho,
Saya sa koi lehraye to lagta hai ki tum ho,
Ab tumhi batao tum kya kisi bhoot se kam ho?


Dil ka dard dil todne wale kya jane,
Pyar ke rivazon ko zamana kya jane,
Hoti hai kitni takleef ladki patane mein,
Ye ghar pe baitha ladki ka baap kya jaane.


Khamoshiyan teri mujhse baatein karti hain, meri har aah har dard
samajhti hain. Pata hai majbuur hai tu bhi aur main bhi, phir bhi
aankhein tere deedar ko tarasti hain.