Zend Framework 1.8 Web Application Development - Packt Publishing

0 downloads 225 Views 1MB Size Report
way to build PHP applications and to assist in rapid application development ... introduction of tooling components; the
Zend Framework 1.8 Web Application Development

Keith Pope

Chapter No. 7 "The Shopping Cart"

In this package, you will find: A Biography of the author of the book A preview chapter from the book, Chapter NO.7 "The Shopping Cart" A synopsis of the book’s content Information on where to buy this book

About the Author Keith Pope has over ten years of experience in web-related industries and has had a keen interest in programming from an early age. Keith currently works in the airline industry as a technical project manager, providing entertainment systems for aircraft. He has been working with the Zend Framework since its first preview release, using it in many of his work and personal projects. I would like to thank my wife; without her support and patience, this book would not have been possible. I would also like to thank Matthew Weier O'Phinney who has been instrumental in the success of the Zend Framework project as well as giving lots of time to the mailing lists, answering both mine and others questions. The rest of the Zend team for all their hard work while creating a great framework that I could write about. Rob Allen and Alex Mace for general help and support. The technical reviewers and the team at Packt for their hard work in getting everything together. Derek Au for his bug reports. Big thanks to my family, the Adkins family, Phil Dunsford, Martin Williams, Tom Hoddell, Sally Hoddell, the Allpay team, Francesca Oliveri, Lucy Hughes-Martin, and Rob Whittle; you all supported me in various ways.

For More Information: www.packtpub.com/zend-framework-1-8-web-application-development/book

Zend Framework 1.8 Web Application Development As web developers we are always looking for ways to improve our systems and working practices. We have to move fast and handle ever-changing requirements from our managers, although this is what makes our work so exciting and challenging. A very important tool that can meet today's fast-changing needs is the basic framework you use to build your application. This forms the basis of your application, and if you have a good framework then you should have fewer problems in the future. A good example is Ruby on Rails, a very popular and successful framework. It has certainly gone a long way in popularizing the use of frameworks, especially in the PHP community, with a lot of PHP developers choosing to switch to Ruby. Why? Well Ruby on Rails will provide you with a lot of very good tools and I can see why people are drawn to it. But the PHP communities are never ones to sit around and since the release of PHP5 there has been a surge of new PHP5 frameworks released. So with all these frameworks what's the best? Well, if you bought this book you have probably already chosen to use the Zend Framework. But I would say use whatever tool fits your project best. All the frameworks out there have good and bad points; it is up to you as a web developer to assess your needs and choose your tools.

Brief history and future developments The Zend Framework was first announced at ZendCon in October 2005 as part of Zend's industry-wide PHP Collaboration Project. Its main aim was to provide a standardized way to build PHP applications and to assist in rapid application development using PHP. The first production version was released in July 2007, and included many great features such as the MVC framework, >View Cart

'; return $html; } [ 233 ]

For More Information: www.packtpub.com/zend-framework-1-8-web-application-development/book

The Shopping Cart

The getSummary() method creates the HTML that will be used to display a summary of the cart items and subtotal to the user. This will be displayed below the main category menus. application/modules/storefront/views/helpers/Cart.php public function addForm(Storefront_Resource_Product_Item $product) { $form = $this->cartModel->getForm('cartAdd'); $form->populate(array( 'productId' => $product->productId, 'returnto' => $this->view->url() )); $form->setAction($this->view->url(array( 'controller' => 'cart', 'action' => 'add', 'module' => 'storefront' ), 'default', true )); return $form; }

The addForm() method will return a form for adding a single product to the cart. This method accepts one parameter $product that must be an instance of Storefront_Resource_Product_Item. We will use this to render individual add to cart forms for each product. application/modules/storefront/views/helpers/Cart.php public function cartTable() { $cartTable = $this->cartModel->getForm('cartTable'); $cartTable->setAction($this->view->url(array( 'controller' => 'cart' , 'action' => 'update' ), 'default' )); $qtys = new Zend_Form_SubForm(); foreach($this->cartModel as $item) { $qtys->addElement('text', (string) $item->productId, array( [ 234 ]

For More Information: www.packtpub.com/zend-framework-1-8-web-application-development/book

Chapter 7 'value' => $item->qty, 'belongsTo' => 'quantity', 'style' => 'width: 20px;', 'decorators' => array( 'ViewHelper' ), ) ); } $cartTable->addSubForm($qtys, 'qtys'); // add shipping options $cartTable->addElement('select', 'shipping', array( 'decorators' => array( 'ViewHelper' ), 'MultiOptions' => $this->_getShippingMultiOptions(), 'onChange' => 'this.form.submit();', 'value' => $this->cartModel->getShippingCost() )); return $cartTable; }

The cartTable() method will return the table containing all our cart items, their costs, and totals. This will be used to update items in the cart. We create a subform to dynamically add the cart items quantity elements at runtime. The reason we use a subform is so we can easily get the whole set of quantity fields from the form, and later iterate over them in the View script. The form will need to contain an array of quantity text elements so that we can iterate over them in the updateAction in the controller. To create this array, we pass the belongsTo option to the addElement() method, which will tell the form that these elements are an array with the name quantity. We also set the value of the element to the qty held in the cart item. We also need a way of passing the productId for each cart item. To do this, we set the element name to the productId of the item. This also helps us by providing a unique name for each element (we have to cast this to a string). It will create a set of text form elements like:

[ 235 ]

For More Information: www.packtpub.com/zend-framework-1-8-web-application-development/book

The Shopping Cart

Once we have all the quantity elements in the subform, we then add the whole subform to the main table form using the addSubForm() method. We give this the name of qtys, which we will use in the View script later to retrieve the elements. We also add the shipping options to the main table form. Here, we use the _getShippingMultiOptions() method to populate the select elements options and set the value to the currently selected shipping option of the cart. application/modules/storefront/views/helpers/Cart.php public function formatAmount($amount) { $currency = new Zend_Currency(); return $currency->toCurrency($amount); }

The formatAmount() method is a little helper method we use to display amounts from the Cart. This may not be necessary in the future as there is a proposal for a currency View Helper that we would use instead. application/modules/storefront/views/helpers/Cart.php private function _getShippingMultiOptions() { $currency = new Zend_Currency(); $shipping = new Storefront_Model_Shipping(); $options = array(0 => 'Please Select'); foreach($shipping->getShippingOptions() as $key => $value) { $options["$value"] = $key . ' - ' . $currency>toCurrency($value); } return $options; } }

Our final method is the private _getShippingMultiOptions() method. This is used internally by the cartTable() method to populate the shipping select element's options. This method gets the shipping options from the Shipping Model and creates an array suitable for the multiOptions option.

Cart View scripts Now that we have all the tools created that we will need to build our cart, we can start creating the user interface. [ 236 ]

For More Information: www.packtpub.com/zend-framework-1-8-web-application-development/book

Chapter 7

Cart view.phtml The view.phtml is the View that is rendered by the viewAction of the CartController. This View includes a title and renders the cartTable form. application/modules/storefront/views/scripts/cart/view.phtml shopping cart

Cart _cart.phtml The ViewScript decorator attached to the table form will render the _cart.phtml View. When it renders, the ViewScript decorator will create a view partial and pass in the form as the element property for this View script. application/modules/storefront/views/scripts/cart/_cart.phtml [ 237 ]

For More Information: www.packtpub.com/zend-framework-1-8-web-application-development/book

The Shopping Cart
SubTotal:
Shipping:
Total:


The HTML produced by this script will look similar to the following screenshot:

The main aspect here is the line items. We need to iterate over the cart and display each product line item. [ 238 ]

For More Information: www.packtpub.com/zend-framework-1-8-web-application-development/book

Chapter 7

Here, we get the Cart Model from the form using our new getModel() method that we created earlier in the SF_Form_Abstract and iterate over it. As we iterate over the Cart Model, we display all the products and line costs. We also get the quantity form elements. To retrieve the correct quantity form element for each product, we access the qtys subform and use the getElement() method. We pass in the items productId as we named our quantity form elements using the productId earlier. All of the other form > in this category