Tải bản đầy đủ (.pdf) (71 trang)

giáo trình joomla 1.5 Toàn tập

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.09 MB, 71 trang )

Ebook: L
ập tr
ình Joomla!
1.5


Zendvn Group

Chương 1: Phân tích & xây d
ựng hệ thống Back
-
End





Gi
ảng vi
ên: Ph
ạm Vũ Kh
ánh

1

Email:






Joomla! Programming
Book shopping component


Chương 1: Phân tích & xây dựng hệ thống Back-End


1. Phân tích Database
2. Xây dựng cấu trúc MVC trong Back-End của Joomla
3. Đăng ký com_book trong hệ thống joomla
4. Tạo các controller cho component
5. Tạo submenu cho com_book
6. Tạo lệnh điều hướng đến các Controller
7. Xây dựng các class định nghĩa các bảng dữ liệu có trong component








Giáo trình: Joomla! Programming
Chuyên đề: Book shopping component

Biên soạn: Phạm Vũ Khánh
Email:
Điện thoại: 0908.893326
Website: www.zend.vn
Tháng 08-2010


Ebook: L
ập tr
ình Joomla!
1.5


Zendvn Group

Chương 1: Phân tích & xây d
ựng hệ thống Back
-
End





Gi
ảng vi
ên: Ph
ạm Vũ Kh
ánh

2

Email:





1. Phân tích Database

Bảng jos_book_category
Bảng chứa các danh mục sách (VD: PHP, ASP, Joomla!, Drupal …)
Fields Type Decription
id int(11) Khóa chính
category varchar(200) Tên của category
created_by int(11) ID người tạo category
created datetime Ngày tạo
modified_by int(11) ID người chỉnh sửa category
modified datetime Ngày modify
ordering int(11) Sắp xếp thứ tự
hits int(11) Số lần xem
published tinyint(1) Cho phép hiển 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 ;


Bảng jos_book
Bảng chứa thông tin của sách theo từng các danh mục
Fields Type Decription
id int(11) Khóa chính
title varchar(200) Tiêu đề quyển sách
picture varchar(50) Hình ảnh quyển sách
publish_date datetime Ngày xuất bản
synopsis text Tóm tắt nội dung
author varchar(150) Tác giả
price varchar(50) Giá bạn
publisher varchar(255) Nhà xuất bản
content mediumtext Nội dung chính
created_by int(11) ID người tạo (Khóa ngoại)
created datetime Ngày tạo
modified_by int(11) ID người chỉnh sửa (Khóa ngoại)
modified datetime Ngày chỉnh sửa
hits int(11) Số lần xem
published tinyint(1) Cho phép hiển thị
Ebook: L
ập tr
ình Joomla!
1.5


Zendvn Group

Chương 1: Phân tích & xây d
ựng hệ thống Back
-
End






Gi
ảng vi
ên: Ph
ạm Vũ Kh
ánh

3

Email:


catid int(11) ID của category (Khóa ngoại)

SQL query:
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 ;

Bảng jos_book_bill
Bảng chứa thông tin khách hàng
Fields Type Decription
id int(11) Khóa chính
created datetime Ngày tạo ra đơn đặt hàng
full_name varchar(255) Tên người mua
address text Địa chỉ người mua
shipping text Địa chỉ giao hàng
phone varchar(20) Số điện thoại
mobi varchar(20) Số điện thoại di động
comment text Thông tin
status varchar(20) Trạng thái (pending – accept)

SQL query:
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 ;

Bảng jos_book_order
Bảng chứa thông tin danh sách các cuốn sách được đặt hàng
Fields Type Decription
id int(11) Khóa chính của bảng
bill_id int(11) ID của jos_book_bill
book_id int(11) ID của cuốn sách
Ebook: L
ập tr
ình Joomla!
1.5


Zendvn Group

Chương 1: Phân tích & xây d
ựng hệ thống Back
-
End





Gi

ảng vi
ên: Ph
ạm Vũ Kh
ánh

4

Email:


quantity int(11) Số lượng sách đặt mua
price varchar(20) Giá của một cuốn sách

SQL query:
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 ;



Ebook: L
ập tr
ình Joomla!
1.5



Zendvn Group

Chương 1: Phân tích & xây d
ựng hệ thống Back
-
End





Gi
ảng vi
ên: Ph
ạm Vũ Kh
ánh

5

Email:


2. Xây dựng cấu trúc MVC cho Back-End của Joomla

Tạo thư mục \com_book trong thư mục \joomla\administrator\components\ có cấu trúc
như sau:


Thư mục Giải thích

\com_book\books Thư mục chứa hình ảnh những cuốn sách
\com_book\controllers Thư mục chứa các controller của component
\com_book\helper Thư mục chứa các tập tin hỗ trợ cho component (vd: nhưng
Upload class, Image functions…)
\com_book\tables Thư mục chứa các tập tin định nghĩa cho các Table của
component
\com_book\views Thư mục chứ các tập tin VIEW theo mô hình MVC của
Joomla
admin.book.php Tập tin chạy chính của component trong phần Back-End

3. Đăng ký com_book trong hệ thống joomla



Vào phpMyAdmin truy cập database của Joomla
Mở bảng jos_components. Nhấn nút Insert, sau đó thêm vào dữ liệu như sau

Ebook: L
ập tr
ình Joomla!
1.5


Zendvn Group

Chương 1: Phân tích & xây d
ựng hệ thống Back
-
End






Gi
ảng vi
ên: Ph
ạm Vũ Kh
ánh

6

Email:



Thêm cho Menu chính (Components menu | Book)


Thêm cho Menu phụ (Components menu | Book | Categories)


Thêm cho Menu phụ (Components menu | Book | Books)

Ebook: L
ập tr
ình Joomla!
1.5



Zendvn Group

Chương 1: Phân tích & xây d
ựng hệ thống Back
-
End





Gi
ảng vi
ên: Ph
ạm Vũ Kh
ánh

7

Email:



Thêm cho Menu phụ (Components menu | Book | Orders)

4. Tạo các controller cho component

Tạo controller quản lý category: Tạo tập tin category.php trong
\joomla\administrator\components\com_book\controllers\ với nội dung sau:


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

class BookControllerCategory extends JController{

function __construct( $default = array() ) {
parent::__construct( $default );
}
}
?>

Tạo controller quản lý book: Tạo tập tin book.php trong
\joomla\administrator\components\com_book\controllers\ với nội dung sau:

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

class BookControllerBook extends JController{

function __construct( $default = array() ) {
parent::__construct( $default );
}

}


?>

Tạo controller quản lý các hóa đơn đặt hàng: Tạo tập tin order.php trong
\joomla\administrator\components\com_book\controllers\ với nội dung sau:
Ebook: L
ập tr
ình Joomla!
1.5


Zendvn Group

Chương 1: Phân tích & xây d
ựng hệ thống Back
-
End





Gi
ảng vi
ên: Ph
ạm Vũ Kh
ánh

8


Email:



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

class BookControllerOrder extends JController{

function __construct( $default = array() ) {
parent::__construct( $default );
}

}
?>

Để truy cập vào các controller của com_book chúng ta quy ước đường dẫn như sau:

Truy cập category controller:
http://localhost/joomla/administrator/index.php?option=com_book

Truy cập book controller:
http://localhost/joomla/administrator/index.php?option=com_book&c=Book

Truy cập order controller:
http://localhost/joomla/administrator/index.php?option=com_book&c=Order


5. Tạo submenu cho com_book



Mở tập tin admin.book.php ở Back-End com_book thêm vào nội 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' );
Ebook: L
ập tr
ình Joomla!
1.5



Zendvn Group

Chương 1: Phân tích & xây d
ựng hệ thống Back
-
End





Gi
ảng vi
ên: Ph
ạm Vũ Kh
ánh

9

Email:


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

?>


6. Tạo lệnh điều hướng đến các Controller

Mở tập tin admin.book.php ở Back-End com_book thêm vào nội 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 thời thêm vào các class BookControllerBook – BookControllerCategory –
BookControllerOrder một phương thức mới có nội dung sau:

function display(){
echo __FUNCTION__ . ' in ' . __CLASS__;
}

7. Xây dựng các class định nghĩa các bảng dữ liệu có trong component


a. Tạo tập tin category.php

Tạo tập tin category.php trong thư mục
\joomla\administrator\components\com_book\tables\. Tập tin này chứa cấu trúc của bảng
jos_book_category và những giá trị mặc định khi chúng ta sử dụng các field trong bảng
với nội 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 );
}
}
Ebook: L
ập tr
ình Joomla!
1.5



Zendvn Group

Chương 1: Phân tích & xây d
ựng hệ thống Back
-
End





Gi
ảng vi
ên: Ph
ạm Vũ Kh
ánh

10

Email:



b. Tạo tập tin book.php

Tạo tập tin book.php trong thư mục \joomla\administrator\components\com_book\tables\.
Tập tin này chứa cấu trúc của bảng jos_book và những giá trị mặc định khi chúng ta sử
dụng các field trong bảng với nội 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. Tạo tập tin bill.php

Tạo tập tin bill.php trong thư mục \joomla\administrator\components\com_book\tables\.
Tập tin này chứa cấu trúc của bảng jos_book_bill và những giá trị mặc định khi chúng ta

sử dụng các field trong bảng với nội 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. Tạo tập tin order.php

Ebook: L
ập tr
ình Joomla!
1.5


Zendvn Group


Chương 1: Phân tích & xây d
ựng hệ thống Back
-
End





Gi
ảng vi
ên: Ph
ạm Vũ Kh
ánh

11

Email:


Tạo tập tin order.php trong thư mục
\joomla\administrator\components\com_book\tables\. Tập tin này chứa cấu trúc của bảng
jos_book_order và những giá trị mặc định khi chúng ta sử dụng các field trong bảng với
nội 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 );
}
}









Ebook
:

L
ập tr
ình Joomla
!
1.5


Zendvn Group


Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph
ạm Vũ Khánh

1

Email:





Joomla! Programming
Book shopping component


Chương 2: Xây dựng Category controller


1. Xác định các chức năng (task) trong Category

2. Xây dựng hệ thống hàm xử lý các chức năng của Category
3. Xây dựng chức năng hiển thị category (task: show)
4. Đăng ký các chức năng xử lý trong Controller
5. Xây dựng chức năng publish & unpublish category (task: publish – unpublish)
6. Xây dựng chức năng delete (task: remove)
7. Xây dựng chức năng thêm một category (task: add – save)
8. Xây dựng chức năng hủy nhiệm vụ (task: cancel)
9. Xây dựng chức năng chỉnh sửa một category (task: edit – save - apply )








Giáo trình: Joomla! Programming
Chuyên đề: Book shopping component

Biên soạn: Phạm Vũ Khánh
Email:
Điện thoại: 0908.893326
Website: www.zend.vn
Tháng 08-2010
Ebook
:

L
ập tr
ình Joomla

!
1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph
ạm Vũ Khánh

2

Email:




1. Xác định task trong BookControllerCategory class

Task Vấn đề xử lý
show Hiển thị các book category. Trong quá trình hiển thị sẽ có nhiều cách

hiển thị khác nhau như:
- Hiển thị theo chiều của các cột (từ A-Z và ngược lại)
- Hiển thị theo từ khóa tìm kiếm
- Hiển thị theo số lượng Category trên một trang
- Hiển thị theo trạng thái publish hoặc unpublish
add Tạo Form để nhập category mới
edit Tạo Form chứa thông tin của category muốn chỉnh sửa
save Lưu dữ liệu vào database
apply Lưu dữ liệu vào database và quay lại Form
unpublish Không cho category hiển thị ở ngoài Front-End
publish Cho phép category hiển thị ngoài Front-End
cancel Tạm hoãn một task nào đó
remove Xóa bỏ một hoặc nhiều Category trong database

2. Xây dựng hệ thống function cho BookControllerCategory class

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

class BookControllerCategory extends JController{

function __construct( $default = array() ) {
parent::__construct( $default );
$this->registerDefaultTask('show');
}

function show(){

echo __FUNCTION__ . ' in ' . __CLASS__;
}

function add(){
echo __FUNCTION__ . ' in ' . __CLASS__;
}

function edit(){
echo __FUNCTION__ . ' in ' . __CLASS__;
}

function save(){
echo __FUNCTION__ . ' in ' . __CLASS__;
}

function apply(){
echo __FUNCTION__ . ' in ' . __CLASS__;
}

function changStatus(){
echo __FUNCTION__ . ' in ' . __CLASS__;
}

Ebook
:

L
ập tr
ình Joomla
!

1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph
ạm Vũ Khánh

3

Email:


function cancel(){
echo __FUNCTION__ . ' in ' . __CLASS__;
}

function remove(){
echo __FUNCTION__ . ' in ' . __CLASS__;
}


function dump($val){
echo '<pre>';
print_r($val);
echo '</pre>';
}
}

?>

3. Xây dựng năng hiển thị các category (task: show)

Để xây dựng chức năng này chúng ta cần thực hiện những công việc sau:
- Xây dựng VIEW cho chức năng show
- Xây dựng hệ thống nút nhấn cho chức năng show
- Lấy dữ liệu từ database hiển thị theo các kiểu:
o Hiển thị theo từ khóa tìm kiếm
o Hiển thị danh sách theo giá trị của các cột trong câu truy vấn
o Hiển thị theo trạng thái publish hoặc unpublish
- Hệ thống phân trang cho danh sách các category.


(Luồng xử lý trong chức năng show)


a. Xây dựng VIEW cho chức năng show()
Ebook
:

L

ập tr
ình Joomla
!
1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph
ạm Vũ Khánh

4

Email:



Tạo tập tin category.php trong thư mục /com_book/views. Tập tin này tương ứng với
category controller của component. Tập tin có nội dung như sau:


class BookViewCategory{
function show(){
echo '<br>' . __FUNCTION__ . ' in ' . __CLASS__;
}

}

Tạo một tập tin template có tên show.php tương ứng với hàm show() trong lớp
BookViewCategory trong thư mục /com_book/views/templates/category với nội dung:

echo '<br> template of show() in VIEW';

Nhúng tập tin VIEW và tập tin CONTROLLER. Mở tập tin category.php trong thư
mục /administrator/components/com_book/controllers thêm vào nội dung sau:

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

require_once( JPATH_COMPONENT.DS.'views'. DS .'category.php' );

class BookControllerCategory extends JController{
//Code goes here
}

Gọi hàm show() trong VIEW vào hàm show() CONTROLLER. Mở tập tin
category.php trong thư mục /com_book/controllers thêm vào nội dung sau:


function show(){
echo __FUNCTION__ . ' in ' . __CLASS__;
BookViewCategory::show();
}

b. Xây dựng hệ thống nút nhấn cho chức năng show


(H003-b1)

Mở tập tin category.php trong thư mục /administrator/components/com_book/views.
Thêm vào hàm setBookCategoryToolBar() với nội dung như sau:

function setBookCategoryToolBar(){
JToolBarHelper::title( JText::_('Book Category manager'), 'generic.png' );
Ebook
:

L
ập tr
ình Joomla
!
1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller






Gi
ảng vi
ên
: Ph
ạm Vũ Khánh

5

Email:


JToolBarHelper::publishList();
JToolBarHelper::unpublishList();
JToolBarHelper::deleteList();
JToolBarHelper::editListX();
JToolBarHelper::addNewX();
}

Sau đó sửa hàm show() trên tập tin này thành:

function show(){
BookViewCategory::setBookCategoryToolBar();

}

Vào chạy thử chúng ta sẽ có giao diện như hình H003-b1


Để hệ thống nút vừa tạo ra có thể hoạt động được chúng ta cần xây dựng một tập tin
show.php trong thư mục /com_book/views/templates/category có nội dung như sau:

<form action="" method="post" name="adminForm">

<input type="hidden" name="option" value="com_book" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0">
</form>



Chú ý: Cách đặt tên Form và 3 tập tin input ẩn

Nhúng template vào VIEW. Để nhúng tập tin show.php vừa tạo vào hàm show() của lớp
BookViewCategory chúng ta cần sửa lại tập tin category.php trong thư mục
/com_book/views/ như sau:

define('TEMPLATE_VIEW',JPATH_COMPONENT.DS.'views'.DS.'templates'.DS.'category');

class BookViewCategory{
function show(){
BookViewCategory::setBookCategoryToolBar();
require_once( TEMPLATE_VIEW.DS.'show.php');
}

}

Sau đó nhấn nút New để thử xem các nút đã hoạt động chưa.


c. Hiển thị danh sách category

Tạo lưới hiển thị dữ liệu danh sách các Category.

c.1: Tạo dòng tiêu đề cho lưới

Ebook
:

L
ập tr
ình Joomla
!
1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph

ạm Vũ Khánh

6

Email:




Mở tập tin show.php trong thư mục /com_book/views/templates/category thêm vào nội
dung như sau:

<?php defined('_JEXEC') or die('Restricted access'); ?>
<form action="" method="post" name="adminForm">

<table class="adminlist" cellspacing="1" width="100%">
<thead>
<tr>
<th width="5">#</th>
<th width="5"><input type="checkbox" name="toggle" value=""></th>
<th class="title">Title</th>
<th width="1%" nowrap="nowrap" class="title">Published</th>
<th width="8%" nowrap="nowrap" class="title">Order</th>
<th width="8%" nowrap="nowrap" class="title">Created Date</th>
<th width="8%" nowrap="nowrap" class="title">Created by</th>
<th width="8%" nowrap="nowrap" class="title">Modified Date</th>
<th width="8%" nowrap="nowrap" class="title">Modified by</th>
<th width="8%" nowrap="nowrap" class="title">Hits</th>
<th width="1%" nowrap="nowrap" class="title">ID</th>
</tr>

</thead>
</table>
<input type="hidden" name="option" value="com_book" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0">
</form>

c.2: Lấy tất cả các category trong database

Mở tập tin category.php trong thư mục /com_book/controllers sửa hàm show() thành:

function show(){
global $mainframe;

$db =& JFactory::getDBO();
$query = 'SELECT c.*, u1.name as postName, u2.name as modifyName
FROM #__book_category as c
LEFT JOIN #__users as u1 ON c.created_by = u1.id
LEFT JOIN #__users as u2 ON c.modified_by = u2.id';
$db->setQuery($query);
$rows = $db->loadObjectList();

BookViewCategory::show();
}

c.3: Truyền giá trị của câu truy vấn ra ngoài VIEW và Template

Ebook
:


L
ập tr
ình Joomla
!
1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph
ạm Vũ Khánh

7

Email:




Mở tập tin category.php trong thư mục /com_book views sửa hàm show() thành:


function show($rows){
BookViewCategory::setBookCategoryToolBar();
require_once( TEMPLATE_VIEW.DS.'show.php');
}

Mở tập tin Category.php trong thư mục /com_book/controllers sửa hàm show() thành:

function show(){
//Code cũ
BookViewCategory::show($rows);
}

Hiển thị giá trị ngoài tập tin template. Mở tập tin show.php trong thư mục
/com_book/views/templates/category thêm vào nội dung như sau:

//Code cũ …
<table class="adminlist" cellspacing="1" width="100%">
<thead>
<tr>
<th width="5">#</th>
<th width="5"><input type="checkbox" name="toggle" value=""
onclick="checkAll(<?php echo count($rows);?>)"></th>
//Code cũ …
</thead>
<?php
$i = 0;
foreach ($rows as $key) {
$checked = JHTML::_('grid.id', $i, $key->id);
$published = JHTML::_('grid.published', $key, $i);

?>
<tr>
<td align="center"><?php echo $i + 1; ?></td>
<td align="center"><?php echo $checked; ?></td>
<td><?php echo $key->category; ?></td>
<td align="center"><?php echo $published; ?></td>
<td align="center"><?php echo $key->ordering; ?></td>
<td align="center"><?php echo $key->created; ?></td>
<td align="center"><?php echo $key->postName; ?></td>
<td align="center"><?php echo $key->modified; ?></td>
<td align="center"><?php echo $key->modifyName; ?></td>
<td align="center"><?php echo $key->hits; ?></td>
<td align="center"><?php echo $key->id; ?></td>
Ebook
:

L
ập tr
ình Joomla
!
1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller






Gi
ảng vi
ên
: Ph
ạm Vũ Khánh

8

Email:


</tr>
<?php
$i++;
}
?>
</table>
//Code cũ …

c.4: Hiển thị danh sách theo từ khóa tìm kiếm



Hiển thị giá trị ngoài tập tin template. Mở tập tin show.php trong thư mục
/com_book/views/templates/category thêm vào nội dung như sau:

<?php defined('_JEXEC') or die('Restricted access'); ?>
<form action="" method="post" name="adminForm">

<table width="100%" cellspacing="0">
<tr>
<td>
<?php echo JText::_('Filter'); ?>
<input type="text" name="search" id="search" value="<?php echo
$lists['search']; ?>" class="text_area" onchange="document.adminForm.submit();" />
<button onclick="this.form.submit();"><?php echo JText::_('Go');
?></button>
<button onclick="document.getElementById('search').value='';
this.form.submit();"><?php echo JText::_('Reset'); ?></button>

</td>
</tr>
</table>
<table class="adminlist" cellspacing="1" width="100%">
//Code cũ…

Mở tập tin Category.php trong thư mục /com_book/controllers sửa hàm show() thành:

function show(){
global $mainframe;

//Tao cau truy van cho phan search
$search = JRequest::getVar( 'search', '');
if(trim($search) != ''){
$where = " where LOWER(c.category) LIKE '%". strtolower ($search) . "%'";
}

$db =& JFactory::getDBO();
$query = 'SELECT c.*, u1.name as postName, u2.name as modifyName

FROM #__book_category as c
LEFT JOIN #__users as u1 ON c.created_by = u1.id
LEFT JOIN #__users as u2 ON c.modified_by = u2.id'
. $where;

Ebook
:

L
ập tr
ình Joomla
!
1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph
ạm Vũ Khánh


9

Email:


$db->setQuery($query);
$rows = $db->loadObjectList();

//Tạo mảng giá trị truyền ra lại VIEW
$lists = array();
$lists['search'] = $search;

BookViewCategory::show($rows,$lists);
}

Mở tập tin category.php trong thư mục /com_book/ views sửa hàm show() thành:

function show($rows,$lists){
BookViewCategory::setBookCategoryToolBar();
require_once( TEMPLATE_VIEW.DS.'show.php');
}

c.5: Hiển thị danh sách theo giá trị của các cột trong câu truy vấn



Mở tập tin category.php trong thư mục /com_book /views sửa hàm show() thành:
function show($rows,$lists){
BookViewCategory::setBookCategoryToolBar();


//Tao cac link sap xep theo cac cot
$sortTitle = JHTML::_('grid.sort', JText::_('Title'), 'c.category',
@$lists['order_Dir'],@$lists['order'] );
$sortPublished = JHTML::_('grid.sort', JText::_('Published'),
'c.published', @$lists['order_Dir'],@$lists['order'] );
$sortOrder = JHTML::_('grid.sort', JText::_('Order'), 'c.ordering',
@$lists['order_Dir'],@$lists['order'] );
$sortCreatedDate = JHTML::_('grid.sort', JText::_('Created Date'),
'c.created_date', @$lists['order_Dir'],@$lists['order'] );
$sortCreatedBy = JHTML::_('grid.sort', JText::_('Created by'), 'postName',
@$lists['order_Dir'],@$lists['order'] );
$sortModifiedDate = JHTML::_('grid.sort', JText::_('Modified Date'),
'c.modified_date', @$lists['order_Dir'],@$lists['order'] );
$sortModifiedBy = JHTML::_('grid.sort', JText::_('Modified By'),
'modifyName', @$lists['order_Dir'],@$lists['order'] );
$sortHits = JHTML::_('grid.sort', JText::_('Hits'), 'c.hits',
@$lists['order_Dir'],@$lists['order'] );
$sortID = JHTML::_('grid.sort', JText::_('ID'), 'c.id',
@$lists['order_Dir'],@$lists['order'] );

require_once( TEMPLATE_VIEW.DS.'show.php');
}

Hiển thị giá trị ngoài tập tin template. Mở tập tin show.php trong thư mục
/com_book/views/templates/category sửa thành:
Ebook
:

L
ập tr

ình Joomla
!
1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph
ạm Vũ Khánh

10

Email:



//Code cũ .
<table class="adminlist" cellspacing="1" width="100%">
<thead>
<tr>

<th width="5">#</th>
<th width="5"><input type="checkbox" name="toggle" value=""
onclick="checkAll(<?php echo count($rows) ?>)"></th>
<th class="title"><?php echo $sortTitle;?></th>
<th width="1%" nowrap="nowrap" class="title"> <?php echo
$sortPublished;?></th>
<th width="8%" nowrap="nowrap" class="title"> <?php echo
$sortOrder;?></th>
<th width="8%" nowrap="nowrap" class="title"> <?php echo
$sortCreatedDate;?></th>
<th width="8%" nowrap="nowrap" class="title"> <?php echo
$sortCreatedBy;?></th>
<th width="8%" nowrap="nowrap" class="title"> <?php echo
$sortModifiedDate;?></th>
<th width="8%" nowrap="nowrap" class="title"> <?php echo
$sortModifiedBy;?></th>
<th width="1%" nowrap="nowrap" class="title"> <?php echo
$sortHits;?></th>
<th width="1%" nowrap="nowrap" class="title"> <?php echo $sortID;?></th>
</tr>
</thead>
//Code cũ …
<input type="hidden" name="option" value="com_book" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0">
<input type="hidden" name="filter_order" value="<?php echo $lists['order']; ?>" />
<input type="hidden" name="filter_order_Dir" value="<?php echo$lists['order_Dir']; ?>" />
</form>

Mở tập tin Category.php trong thư mục /com_book/controllers sửa hàm show() thành:


function show(){
global $mainframe;

//Code cũ . . .

//Tao cau truy van cho phan sap xep theo cot
$filter_order = JRequest::getVar( 'filter_order', 'c.id');
$filter_order_Dir = JRequest::getVar( 'filter_order_Dir', 'desc');
$order = ' ORDER BY ' . $filter_order . ' ' .$filter_order_Dir;

$db =& JFactory::getDBO();
$query = 'SELECT c.*, u1.name as postName, u2.name as modifyName
FROM #__book_category as c
LEFT JOIN #__users as u1 ON c.created_by = u1.id
LEFT JOIN #__users as u2 ON c.modified_by = u2.id'
. $where . $order;

$db->setQuery($query);
$rows = $db->loadObjectList();

//Tạo mảng giá trị truyền ra lại VIEW
$lists = array();
$lists['search'] = $search;
$lists['order_Dir'] = $filter_order_Dir;
$lists['order'] = $filter_order;

BookViewCategory::show($rows,$lists);
}


c.6: Hiển thị phân trang

Ebook
:

L
ập tr
ình Joomla
!
1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph
ạm Vũ Khánh

11

Email:





Mở tập tin Category.php trong thư mục /com_book/controllers sửa hàm show() thành:

function show(){
global $mainframe;

//Code cũ . . .

//Tao cau truy van cho tao phan trang
$limit = JRequest::getVar('limit',$mainframe->getCfg('list_limit'));
$limitstart = JRequest::getVar('limitstart',0);
$limitSQL = ' LIMIT ' . $limitstart . ',' . $limit;

$db =& JFactory::getDBO();
$query = 'SELECT c.*, u1.name as postName, u2.name as modifyName
FROM #__book_category as c
LEFT JOIN #__users as u1 ON c.created_by = u1.id
LEFT JOIN #__users as u2 ON c.modified_by = u2.id'
. $where . $order . $limitSQL;
//Code cũ . . .

//Tinh tong so record co trong bang
$sql_1 = 'SELECT COUNT(*) FROM #__book_category as c ' . $where;
$db->setQuery( $sql_1 );
$total = $db->loadResult();

//Goi class phan trang

jimport('joomla.html.pagination');
$pageNav = new JPagination( $total, $limitstart, $limit);

//Code cũ …

BookViewCategory::show($rows,$lists,$pageNav);
}

Mở tập tin category.php trong thư mục /com_book/ views sửa hàm show() thành:

function show($rows,$lists,$pageNav){
BookViewCategory::setBookCategoryToolBar();

//Code cũ

require_once( TEMPLATE_VIEW.DS.'show.php');
}

Hiển thị giá trị ngoài tập tin template. Mở tập tin show.php trong thư mục
/com_book/views/templates/category sửa thành:

//Code cũ …
<?php
$i++;
}
?>
<tfoot>
<tr>
<td colspan="13">
<?php echo $pageNav->getListFooter(); ?>

Ebook
:

L
ập tr
ình Joomla
!
1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph
ạm Vũ Khánh

12

Email:



</td>
</tr>
</tfoot>
</table>
<input type="hidden" name="option" value="com_book" />
<input type="hidden" name="task" value="" />
//Code cũ …

c.7: Hiển thị theo trạng thái publish hoặc unpublish



Mở tập tin Category.php trong thư mục /com_book/controllers sửa hàm show() thành:

function show() {
global $mainframe;

//Tao cau truy van cho phan search
$search = JRequest::getVar('search', '');
if (trim($search) != '') {
$where[] = " where LOWER(c.category) LIKE '%" . strtolower($search) . "%'";
}

$filter_state = JRequest::getVar('filter_state', '');
if ($filter_state) {
if ($filter_state == 'P') {
$where[] = 'c.published = 1';
} else if ($filter_state == 'U') {
$where[] = 'c.published = 0';
}

}

$where = count($where) ? ' WHERE ' . implode(' AND ', $where) : '';
//Code cũ …

$lists['order'] = $filter_order;
$lists['state'] = JHTML::_('grid.state', $filter_state);

BookViewCategory::show($rows, $lists, $pageNav);
}

Hiển thị giá trị ngoài tập tin template. Mở tập tin show.php trong thư mục
/com_book/views/templates/category sửa thành:

<table width="100%" cellspacing="0">
<tr>
<td>
<?php echo JText::_('Filter'); ?>
<input type="text" name="search" id="search" value="<?php echo
$lists['search']; ?>" class="text_area" onchange="document.adminForm.submit();" />
Ebook
:

L
ập tr
ình Joomla
!
1.5



Zendvn Group

Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph
ạm Vũ Khánh

13

Email:


<button onclick="this.form.submit();"><?php echo JText::_('Go');
?></button>
<button onclick="document.getElementById('search').value='';
this.form.submit();"><?php echo JText::_('Reset'); ?></button>

</td>
<td nowrap="nowrap" align="right">
Select: <?php echo $lists['state'];?>
</td>


</tr>
</table>

4. Đăng ký các chức năng xử lý trong Controller

Mở tập tin Category.php trong thư mục /com_book/controllers sửa hàm __construct ()
thành:

function __construct( $default = array() ) {
parent::__construct( $default );
$this->registerDefaultTask('show');
$this->registerTask('add', 'add');
$this->registerTask('edit', 'edit');
$this->registerTask('save', 'save');
$this->registerTask('apply', 'save');
$this->registerTask('publish', 'changStatus');
$this->registerTask('unpublish', 'changStatus');
$this->registerTask('cancel', 'cancel');
$this->registerTask('remove', 'remove');
}

5. Xây dựng chức năng publish & unpublish category (task: publish – unpublish)



Mở tập tin Category.php trong thư mục /com_book/controllers sửa hàm changeStatus()
thành:

function changStatus() {


global $mainframe;

$task = $this->getTask();

if ($task == 'unpublish') {
$state = 0;
} else {
Ebook
:

L
ập tr
ình Joomla
!
1.5


Zendvn Group

Chương 2: Xây d
ựng Category controller





Gi
ảng vi
ên
: Ph

ạm Vũ Khánh

14

Email:


$state = 1;
}

//Goi DBO de tuong tac voi CSDL
$db = & JFactory::getDBO();
//Lay gia tri id cua cac Item can thay trang thai
$cid = JRequest::getVar('cid',array());

if (count($cid) > 0) {
//Chuyen mang ID thanh mot chuoi
$cids = implode(',', $cid);

// Tao cau SQL de cap nhat trang cho Item
$query = 'UPDATE #__book_category' .
' SET published = ' . (int) $state .
' WHERE id IN ( ' . $cids . ' )';

$db->setQuery($query);
if (!$db->query()) {
JError::raiseError(500, $db->getErrorMsg());
}
}


$mainframe->redirect('index.php?option=com_book');
}

6. Xây dựng chức năng delete (task: remove)



Mở tập tin Category.php trong thư mục /com_book/controllers sửa hàm remove() thành:

function remove() {
global $mainframe;
$cid = JRequest::getVar('cid');

$db = & JFactory::getDBO();
if (count($cid) > 0) {
$cids = implode(',', $cid);
$query = 'DELETE FROM #__book_category WHERE id IN ( ' . $cids . ' )';
$db->setQuery($query);

if (!$db->query()) {
JError::raiseError(500, $db->getErrorMsg());
}
}
$mainframe->redirect('index.php?option=com_book');
}

7. Xây dựng chức năng thêm một category (task: add – save)

×