You are on page 1of 11

Ebook: Lp trnh Joomla! 1.

5 Chng 1: Phn tch & xy dng h thng Back-End

Zendvn Group http://www.zend.vn

Joomla! Programming Book shopping component


Chng 1: Phn tch & xy dng h thng Back-End
1. 2. 3. 4. 5. 6. 7. Phn tch Database Xy dng cu trc MVC trong Back-End ca Joomla ng k com_book trong h thng joomla To cc controller cho component To submenu cho com_book To lnh iu hng n cc Controller Xy dng cc class nh ngha cc bng d liu c trong component

Gio trnh: Joomla! Programming Chuyn : Book shopping component Bin son: Phm V Khnh Email: vukhanh2212@gmail.com in thoi: 0908.893326 Website: www.zend.vn Thng 08-2010

Ging vin: Phm V Khnh

Email: vukhanh2212@gmail.com

Ebook: Lp trnh Joomla! 1.5 Chng 1: Phn tch & xy dng h thng Back-End

Zendvn Group http://www.zend.vn

1. Phn tch Database Bng jos_book_category Bng cha cc danh mc sch (VD: PHP, ASP, Joomla!, Drupal ) Fields Type Decription id int(11) Kha chnh category varchar(200) Tn ca category created_by int(11) ID ngi to category created datetime Ngy to modified_by int(11) ID ngi chnh sa category modified datetime Ngy modify ordering int(11) Sp xp th t hits int(11) S ln xem published tinyint(1) Cho php hin th SQL query:
CREATE TABLE IF NOT EXISTS `jos_book_category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `category` varchar(200) NOT NULL, `created_by` int(11) NOT NULL, `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `modified_by` int(11) NOT NULL, `modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `ordering` int(11) NOT NULL, `hits` int(11) NOT NULL, `published` tinyint(1) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Bng jos_book Bng cha thng tin ca sch theo tng cc danh mc Fields Type Decription id int(11) Kha chnh title varchar(200) Tiu quyn sch picture varchar(50) Hnh nh quyn sch publish_date datetime Ngy xut bn synopsis text Tm tt ni dung author varchar(150) Tc gi price varchar(50) Gi bn publisher varchar(255) Nh xut bn content mediumtext Ni dung chnh created_by int(11) ID ngi to (Kha ngoi) created datetime Ngy to modified_by int(11) ID ngi chnh sa (Kha ngoi) modified datetime Ngy chnh sa hits int(11) S ln xem published tinyint(1) Cho php hin th
Ging vin: Phm V Khnh 2 Email: vukhanh2212@gmail.com

Ebook: Lp trnh Joomla! 1.5 Chng 1: Phn tch & xy dng h thng Back-End

Zendvn Group http://www.zend.vn

catid SQL query:

int(11)

ID ca category (Kha ngoi)

CREATE TABLE IF NOT EXISTS `jos_book` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(200) NOT NULL, `picture` varchar(50) NOT NULL, `publish_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `synopsis` text, `author` varchar(150) DEFAULT NULL, `price` varchar(50) NOT NULL, `publisher` varchar(255) DEFAULT NULL, `content` mediumtext NOT NULL, `created_by` int(11) NOT NULL, `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `modified_by` int(11) NOT NULL DEFAULT '0', `modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `hits` int(11) NOT NULL, `published` tinyint(1) NOT NULL DEFAULT '0', `catid` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Bng jos_book_bill Bng cha thng tin khch hng Fields Type id int(11) created datetime full_name varchar(255) address text shipping text phone varchar(20) mobi varchar(20) comment text status varchar(20) SQL query:

Decription Kha chnh Ngy to ra n t hng Tn ngi mua a ch ngi mua a ch giao hng S in thoi S in thoi di ng Thng tin Trng thi (pending accept)

CREATE TABLE IF NOT EXISTS `jos_book_bill` ( `id` int(11) NOT NULL AUTO_INCREMENT, `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `full_name` varchar(255) NOT NULL, `address` text NOT NULL, `shipping` text NOT NULL, `phone` varchar(20) NOT NULL, `mobi` varchar(20) NOT NULL, `comment` text NOT NULL, `status` varchar(20) NOT NULL DEFAULT 'pending', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Bng jos_book_order Bng cha thng tin danh sch cc cun sch c t hng Fields Type Decription id int(11) Kha chnh ca bng bill_id int(11) ID ca jos_book_bill book_id int(11) ID ca cun sch
Ging vin: Phm V Khnh 3 Email: vukhanh2212@gmail.com

Ebook: Lp trnh Joomla! 1.5 Chng 1: Phn tch & xy dng h thng Back-End

Zendvn Group http://www.zend.vn

quantity price SQL query:

int(11) varchar(20)

S lng sch t mua Gi ca mt cun sch

CREATE TABLE IF NOT EXISTS `jos_book_order` ( `id` int(11) NOT NULL AUTO_INCREMENT, `bill_id` int(11) NOT NULL, `book_id` int(11) NOT NULL, `quantity` int(11) NOT NULL, `price` varchar(20) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Ging vin: Phm V Khnh

Email: vukhanh2212@gmail.com

Ebook: Lp trnh Joomla! 1.5 Chng 1: Phn tch & xy dng h thng Back-End

Zendvn Group http://www.zend.vn

2. Xy dng cu trc MVC cho Back-End ca Joomla To th mc \com_book trong th mc \joomla\administrator\components\ c cu trc nh sau:

Th mc \com_book\books \com_book\controllers \com_book\helper \com_book\tables \com_book\views admin.book.php

Gii thch Th mc cha hnh nh nhng cun sch Th mc cha cc controller ca component Th mc cha cc tp tin h tr cho component (vd: nhng Upload class, Image functions) Th mc cha cc tp tin nh ngha cho cc Table ca component Th mc ch cc tp tin VIEW theo m hnh MVC ca Joomla Tp tin chy chnh ca component trong phn Back-End

3. ng k com_book trong h thng joomla

Vo phpMyAdmin truy cp database ca Joomla M bng jos_components. Nhn nt Insert, sau thm vo d liu nh sau

Ging vin: Phm V Khnh

Email: vukhanh2212@gmail.com

Ebook: Lp trnh Joomla! 1.5 Chng 1: Phn tch & xy dng h thng Back-End

Zendvn Group http://www.zend.vn

Thm cho Menu chnh (Components menu | Book)

Thm cho Menu ph (Components menu | Book | Categories)

Thm cho Menu ph (Components menu | Book | Books)

Ging vin: Phm V Khnh

Email: vukhanh2212@gmail.com

Ebook: Lp trnh Joomla! 1.5 Chng 1: Phn tch & xy dng h thng Back-End

Zendvn Group http://www.zend.vn

Thm cho Menu ph (Components menu | Book | Orders) 4. To cc controller cho component To controller qun l category: To tp tin category.php trong \joomla\administrator\components\com_book\controllers\ vi ni dung sau:
<?php // Chong defined( //2. Goi jimport( truy cap truc tiep '_JEXEC' ) or die( 'Restricted access' ); lop ho tro controller 'joomla.application.component.controller' );

class BookControllerCategory extends JController{ function __construct( $default = array() ) { parent::__construct( $default ); } } ?>

To controller qun l book: To tp tin book.php trong \joomla\administrator\components\com_book\controllers\ vi ni dung sau:


<?php // Chong defined( //2. Goi jimport( truy cap truc tiep '_JEXEC' ) or die( 'Restricted access' ); lop ho tro controller 'joomla.application.component.controller' );

class BookControllerBook extends JController{ function __construct( $default = array() ) { parent::__construct( $default ); } } ?>

To controller qun l cc ha n t hng: To tp tin order.php trong \joomla\administrator\components\com_book\controllers\ vi ni dung sau:


Ging vin: Phm V Khnh 7 Email: vukhanh2212@gmail.com

Ebook: Lp trnh Joomla! 1.5 Chng 1: Phn tch & xy dng h thng Back-End

Zendvn Group http://www.zend.vn

<?php // Chong defined( //2. Goi jimport(

truy cap truc tiep '_JEXEC' ) or die( 'Restricted access' ); lop ho tro controller 'joomla.application.component.controller' );

class BookControllerOrder extends JController{ function __construct( $default = array() ) { parent::__construct( $default ); } } ?>

truy cp vo cc controller ca com_book chng ta quy c ng dn nh sau: Truy cp category controller: http://localhost/joomla/administrator/index.php?option=com_book Truy cp book controller: http://localhost/joomla/administrator/index.php?option=com_book&c=Book Truy cp order controller: http://localhost/joomla/administrator/index.php?option=com_book&c=Order 5. To submenu cho com_book

M tp tin admin.book.php Back-End com_book thm vo ni dung:


<?php defined( '_JEXEC' ) or die( 'Restricted access' ); $controllerName = JRequest::getCmd( 'c', 'category' ); if($controllerName == 'category'){ JSubMenuHelper::addEntry(JText::_('Categories'), '',true ); JSubMenuHelper::addEntry(JText::_('Books'), 'index.php?option=com_book&c=Book'); JSubMenuHelper::addEntry(JText::_('Order'), 'index.php?option=com_book&c=Order' ); } if($controllerName == 'book'){ JSubMenuHelper::addEntry(JText::_('Categories'), 'index.php?option=com_book'); JSubMenuHelper::addEntry(JText::_('Books'), '',true ); JSubMenuHelper::addEntry(JText::_('Order'), 'index.php?option=com_book&c=Order' ); } if($controllerName == 'order'){ JSubMenuHelper::addEntry(JText::_('Categories'), 'index.php?option=com_book' );

Ging vin: Phm V Khnh

Email: vukhanh2212@gmail.com

Ebook: Lp trnh Joomla! 1.5 Chng 1: Phn tch & xy dng h thng Back-End

Zendvn Group http://www.zend.vn

JSubMenuHelper::addEntry(JText::_('Books'), 'index.php?option=com_book&c=Book'); JSubMenuHelper::addEntry(JText::_('Order'), '',true ); } ?>

6. To lnh iu hng n cc Controller M tp tin admin.book.php Back-End com_book thm vo ni dung:


<?php //Code c require_once( JPATH_COMPONENT.DS.'controllers'.DS.$controllerName.'.php' ); $controllerName = 'BookController'.$controllerName; // Create the controller $controller = new $controllerName(); // Perform the Request task $controller->execute( JRequest::getCmd('task') ); // Redirect if set by the controller $controller->redirect(); ?>

ng thi thm vo cc class BookControllerBook BookControllerCategory BookControllerOrder mt phng thc mi c ni dung sau:
function display(){ echo __FUNCTION__ . ' in ' . __CLASS__; }

7. Xy dng cc class nh ngha cc bng d liu c trong component a. To tp tin category.php To tp tin category.php trong th mc \joomla\administrator\components\com_book\tables\. Tp tin ny cha cu trc ca bng jos_book_category v nhng gi tr mc nh khi chng ta s dng cc field trong bng vi ni dung nh sau:
defined('_JEXEC') or die('Restricted Access'); class TableCategory extends JTable{ var $id = null; var $category = null; var $created_by = null; var $created = null; var $modified_by = null; var $modified = null; var $ordering = null; var $hits = null; var $published = 0; function __construct(&$db) { parent::__construct( '#__book_category', 'id', $db ); } } Ging vin: Phm V Khnh 9 Email: vukhanh2212@gmail.com

Ebook: Lp trnh Joomla! 1.5 Chng 1: Phn tch & xy dng h thng Back-End

Zendvn Group http://www.zend.vn

b. To tp tin book.php To tp tin book.php trong th mc \joomla\administrator\components\com_book\tables\. Tp tin ny cha cu trc ca bng jos_book v nhng gi tr mc nh khi chng ta s dng cc field trong bng vi ni dung nh sau:
defined('_JEXEC') or die('Restricted Access'); class TableBook extends JTable{ var $id = null; var $title = null; var $picture = null; var $publish_date = null; var $synopsis = null; var $author = null; var $price = null; var $publisher = null; var $content = null; var $created_by = null; var $created = null; var $modified_by = null; var $modified = null; var $hits = null; var $published = null; var $catid = 0; function __construct(&$db) { parent::__construct( '#__book', 'id', $db ); } }

c. To tp tin bill.php To tp tin bill.php trong th mc \joomla\administrator\components\com_book\tables\. Tp tin ny cha cu trc ca bng jos_book_bill v nhng gi tr mc nh khi chng ta s dng cc field trong bng vi ni dung nh sau:
defined('_JEXEC') or die('Restricted Access'); class TableBill extends JTable{ var $id = null; var $created = null; var $full_name = null; var $address = null; var $shipping = null; var $phone = null; var $mobi = null; var $comment = null; var $status = 'pendding'; function __construct(&$db) { parent::__construct( '#__book_bill', 'id', $db ); } }

d. To tp tin order.php

Ging vin: Phm V Khnh

10

Email: vukhanh2212@gmail.com

Ebook: Lp trnh Joomla! 1.5 Chng 1: Phn tch & xy dng h thng Back-End

Zendvn Group http://www.zend.vn

To tp tin order.php trong th mc \joomla\administrator\components\com_book\tables\. Tp tin ny cha cu trc ca bng jos_book_order v nhng gi tr mc nh khi chng ta s dng cc field trong bng vi ni dung nh sau:
defined('_JEXEC') or die('Restricted Access'); class TableOrder extends JTable{ var $id = null; var $bill_id = null; var $book_id = null; var $quantity = null; var $price = null; function __construct(&$db) { parent::__construct( '#__book_order', 'id', $db ); } }

Ging vin: Phm V Khnh

11

Email: vukhanh2212@gmail.com

You might also like