42
JoomlaBasic.Com Page 1 I. GIỚI THIỆU CHUNG 1. Mô hình MVC MVC hay Model-View- Controller (tiếng Anh) là một mẫu kiến trúc phần mềm trong kỹ thuật kỹ sư phần mềm. Khi sử đúng cách, mẫu MVC giúp cho người phát triển phần mềm cô lập các nguyên tắc nghiệp vụ và giao diện người dùng một cách rõ ràng hơn. Phần mềm phát triển theo mẫu MVC tạo nhiều thuận lợi cho việc bảo trì vì các nguyên tắc nghề nghiệp và giao diện ít liên quan với nhau. Trong mẫu Model -View- Controller, mô hình (model) tượng trưng cho dữ liệu của chương trình phần mềm. Tầm nhìn hay khung nhìn (view) bao gồm các thành phần của giao diện người dùng. Bộ kiểm tra hay bộ điều chỉnh (controller) quản lý sự trao đổi giữa dữ liệu và các nguyên tắc nghề nghiệp trong các thao tác liên quan đến mô hình. 2. Công cụ trong quá trình thực hành - Trình soạn thảo Notepad++ bạn có thể download về tại địa chỉ http://notepad-plus-plus.org/ - Máy chủ ứng dụng Wamp download tại địa chỉ http://www.wampserver.com/en/ - Công cụ lập trình PHP & MySQL trong hướng dẫn này chúng ta sẽ sử dụng NetBeans các phiên bản có hỗ trợ PHP bạn có thể tải về tại địa chỉ http://netbeans.org/projects/php/ - Tham khảo thêm các hàm và các meo tại trang chủ của tác giả http://joomlabasic.com

Lap trinh-joomla-15-theo-mo-hinh-mvc

Embed Size (px)

DESCRIPTION

Tài liệu thiết kế và lập trình component cho joomla được viết bởi joomlabasic.com. Tài liệu được viết theo dạng cầm tay chỉ việc nên dễ dàng tiếp cận

Citation preview

Page 1: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 1

I. GIỚI THIỆU CHUNG

1. Mô hình MVC

MVC hay Model-View-Controller (tiếng Anh) là một mẫu kiến trúc phần mềm trong kỹ thuật kỹ sư phần mềm. Khi sử đúng cách, mẫu MVC giúp cho người phát triển phần mềm cô lập các nguyên tắc nghiệp vụ và giao diện người dùng một cách rõ ràng hơn. Phần mềm phát triển theo

mẫu MVC tạo nhiều thuận lợi cho việc bảo trì vì các nguyên tắc nghề nghiệp và giao diện ít liên quan với nhau.

Trong mẫu Model-View-Controller, mô hình (model) tượng trưng cho dữ liệu của chương trình phần mềm. Tầm nhìn hay khung nhìn (view) bao gồm các thành phần của giao diện người dùng.

Bộ kiểm tra hay bộ điều chỉnh (controller) quản lý sự trao đổi giữa dữ liệu và các nguyên tắc nghề nghiệp trong các thao tác liên quan đến mô hình.

2. Công cụ trong quá trình thực hành

- Trình soạn thảo Notepad++ bạn có thể download về tại địa chỉ http://notepad-plus-plus.org/ - Máy chủ ứng dụng Wamp download tại địa chỉ http://www.wampserver.com/en/

- Công cụ lập trình PHP & MySQL trong hướng dẫn này chúng ta sẽ sử dụng NetBeans các phiên bản có hỗ trợ PHP bạn có thể tải về tại địa chỉ http://netbeans.org/projects/php/ - Tham khảo thêm các hàm và các meo tại trang chủ của tác giả http://joomlabasic.com

Page 2: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 2

II. CẤU TRÚC CỦA MỘT COMPONENT

Cấu trúc tổng quát của một Component trong Joomla ! CMS sẽ được xây dựng như hình bên dưới

Hình 1

Page 3: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 3

Hình 2

- Hình 1: Là cấu trúc của component quản trị trong Joomla ở thưc mục yoursite.com/administrator/components

- Hình 2: Là cấu trúc của một component bên ngoài yoursite.com/components

- Điểm khác nhau chính giữa 2 cấu trúc này là điểm vào của component trong quản trị là admin.hoidap.php trong khi bên ngoài là hoidap.php bạn nên lưu ý kỹ phần này.

Page 4: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 4

1. XÂY DỰNG COMPONENT TRONG QUẢN TRỊ 1.1 THÊM MENU COMPONENT VÀO TRÌNH ĐƠN

Để thực hiện điều này bạn vào yourdomain/phpmyadmin thêm các dòng này vào bảng

jos_components như hình bên dưới.

Ý nghĩa của từng trường:

- id : là định danh của từng dòng, mục này bạn bỏ trống.

- name: tên của component - link: là đường dẫn đến component của chúng ta, quy tắc chung option=<tên thư mục

component>&view=<Các view tương ứng trong thư mục view> - menuid mục này bạn để trống. - parent cha hiện tại, dựa vào id.

- admin_menu_link: tương tự như mục link - admin_menu_alt: tên thay thế theo link

- option: điền tên component của chúng ta. - ordering để sắp xếp thứ tự trước sau cho sub menu . - admin_menu_img hình ảnh đại diện menu.

- iscore trường này mặc định, bỏ trống. - params các tham số, nếu không dùng thì bạn bỏ trống.

- enabled có hiệu dụng không 1 có 0 không.

Page 5: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 5

Tạo menu chính cho component

Page 6: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 6

Tạo các submenu chúng ta tạo tương tự nhung chú ý trường parent và ordering.

Page 7: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 7

Do trình mục này chúng ta xếp ordering là 2 nên nó sẽ sắp sau, các hàng trong component có ordering là 1.

1.2 XÂY DỰNG CSDL CHO COMPONENT

Page 8: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 8

Sau khi tạo xong trình đơn, chúng ta bắt tay vào xây dựng CSDL cho component. Ở đây chúng tôi đã tạo ra 2 bảng CSDL mẫu là bảng jos_hoidap_cauhoi và bảng jos_hoidap_dapan cấu trúc

2 bảng này như sau:

Bảng jos_hoidap_cauhoi

Bảng jos_hoidap_dapan

Ở đây tôi dùng chương trình Navicat for MySQL để tạo CSDL như 2 hình trên, bạn có thể tham tải về tại http://dangky.phpcantho.com

1.3 XÂY DỰNG ĐIỂM VÀO CHO COMPONENT admin.hoidap.php

- Do component của chúng ta sẽ thực hiện nhiều tác vụ như SAVE, APPLY, PUBLISH…vì thế

chúng cần 1 người dẫn đường chỉ chúng biết phải làm gì, ở đâu và làm như thế nào. Thì tập tin admin.hoidap.php sẽ đảm nhiệm vai trò này.

- Nhiệm vụ chính của tập tin này là cho bạn biết bạn đang ở Controller nào, và nó sẽ lắng nghe các yêu cầu (task) và thực thi yêu cầu đó ứng với các Controller đó.

Page 9: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 9

Ý nghĩa của từng dòng lệnh:

- Dòng 3, dùng để cấm người dùng truy cập trực tiếp vào chính của chúng ta, ví dụ khi họ truy

cập vào yourdomain/administrator/components/admin.hoidap.php thì sẽ nhận được câu thông báo “Restricted access”.

- Dòng 8, dùng để nhận tên của Controller. - Dòng 16, dùng để thêm controller của chúng ta. - Dòng 20, khởi tạo lớp hàm để sử dụng chúng.

- Dòng 23, nhận vào các task (tác vụ ) và thực thi chúng. - Dòng 26, chuyển hướng theo tác vụ.

1.4 XÂY DỰNG CÁC LỚP JTABLE CHO COMPONENT

- Lớp hàm JTable này là một lớp hàm để khai báo cấu trúc các trường trong CSDL chúng ta.

Cách đặt tên cho các tập tin nằm trong lớp hàm này như sau:

Page 10: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 10

Class JTable<Tên tập tin> extends JTable

{

// Khai báo bên trong đây

}

- Các tập tin này nằm trong folder tables của component chúng ta tại đường dẫn yourdomain/administrator/components/com_hoidap/tables , chúng sẽ khai báo cho joomla

biết cách dữ liệu được tổ chức ra sao để thực hiện các nâng cập nhật, thêm, sửa , xóa trên dữ liệu sau này. Chúng ta tạo 2 file cauhoi.php và dapan.php nằm trong thư mục tables này.

Nội dung file cauhoi.php

Nội dung file dapan.php

Page 11: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 11

Class parent::__construct(<Tên bản trong CSDL>,<Khóa chính>,<Tham số truyền vào là

một đối tượng lớp JDatabase>);

Class JFactory là lớp hàm tạo ra các phương thức để chúng ta xử lý và thao tác.

JFactory::getDate()->toMySQL() : để nhận về ngày theo dạng CSDL chúng ta theo dang 0000-00-00 00:00:00

JFactory::getUser()->get(‘id’): để nhận về id hiện tại của người đang thao tác trên CSDL.

1.5 XÂY DỰNG MODEL ĐỂ THAO TÁC TRÊN CSDL

Để xây dựng Model thao tác trên CSDL bạn cần truy cập vào component hiện tại tạo mới 2 file cauhoi.php và dapan.php chúng nằm trong thư mục models trong component của chúng ta.

Nội dung của file cauhoi.php

Page 12: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 12

- Dòng 4, dùng để import các model từ thư viện của joomla. - Dòng 5, class HoidapModelCauhoi, quy tắc đặt tên <Tên component bỏ phần tiên tố com_

>Model<Tên của Model>

- Xét các hàm chức năng getTotal() để nhận về tổng số mẫu tin có trong CSDL của bảng câu hỏi, mục đích chính để chúng ta phân trang trong phần view.html.php trong thư mục views.

- Hàm showCauhoi($limitstart, $limit) dùng để hiện thị mẫu tin ra giống như hình sau:

Page 13: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 13

Trong đó bạn cần chú ý đến hàm $this->_getList($query, $limitstart, $limit) hàm này trả về

các mẫu tin được giới hạn từ $limitstart đến $limit. Nếu bạn bỏ qua 2 tham số sau cùng thì sẽ trả về tất cả các mẫu tin.

Hàm editCauhoi() dùng để load mẫu tin theo id của chúng. Cách sử dụng hàm getVar và setVar

JRequest::getVar() trả về giá trị nhận được từ biến. JRequest::setVar() gán giá trị cho biến. JRequest::setVar(„hiddenmainmenu‟,1) để ẩn trình đơn ngang của menu trong quản trị.

- Các bước sử dụng lớp hàm của JTable:

Bước 1: Tạo đường dẫn tới thư mục tables

Bước 2: Khởi tạo lớp hàm JTable

Bước 3: Sử dụng hàm trong lớp JTable một số hàm của lớp JTable

JRequest::setVar(<Tên biến>,<Giá trị>)

JTable::getInstance(<Tên tập tin trong thư mục tables>); // Khởi tạo lớp hàm JTable

JTable::addIncludePath(<Đường dẫn tới thư mục tables>);

JRequest::getVar(<Tên biến>,<Giá trị mặc định>,<Phương thức>,<Kiểu biến>)

Page 14: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 14

Nội dung của file dapan.php

bind( $from, $ignore=array() ) // Trả về True nếu dữ liệu được gắn vào Table

load( $oid=null ) // Load 1 dòng với $oid, trả về True nếu thành công.

store( $updateNulls=false ) // Lưu dữ liệu

move( $dirn, $where='' ) // Thay đổi thứ tự của tập tin

getNextOrder ( $where='' )

delete( $oid=null )

canDelete( $oid=null, $joins=null )

checkout( $who, $oid = null )

checkin( $oid=null )

hit( $oid=null, $log=false )

isCheckedOut( $with = 0, $against = null)

save( $source, $order_filter='', $ignore='' )

publish( $cid=null, $publish=1, $user_id=0 )

toXML( $mapKeysToText=false )

addIncludePath( $path=null )

check()

reset()

getInstance( $type, $prefix = 'JTable', $config = array() )

Page 15: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 15

Page 16: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 16

Hàm getTotal() trả về tổng số dòng có trong bảng jos_hoidap_dapan

Hàm showDapan($where, $limitstart, $limit) để nhận về các các dòng trong bảng theo điều kiện

truyền trong biến $where

Hàm cauhoiRef( $name, $active = NULL, $javascript = NULL, $size = 1, $sel_cat = 1 ) trả về id, title của câu hỏi tồn tại trong bảng đáp án theo dạng danh sách.

Cú pháp sử dụng HTML::_();

Hàm getCauhoi($sel) trả về danh sách các câu hỏi

JHTML::_(„select.genericlist‟,<mảng>,<Tên Selectbox>,<Kiểu trang trí (STYLE)

>,<Giá trị>,<Tiêu đề>,<Giá trị mặc định> )

Page 17: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 17

Hàm editDapan() trả về dữ liệu theo id đầu tiên nhận được từ hàm JRequest::getVar().

Cú pháp của hàm JError::raiseError() hàm này trả về dòng thông báo lỗi.

1.6 THAO TÁC TRÊN VIEWS

Sau khi hoàn tất các hàm cần thiết trên model chúng ta chuyển sang thao tác với views. Đầu tiên tại thư mục views ta tạo các cấu trúc như sau :

JError::raiseError(<Mã lỗi>,<Dòng thông báo>);

Page 18: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 18

Trong tập tin view.html.php tại thư mục views/cauhoi/ ta tạo lớp HoidapViewCauhoi như hình bên

dưới :

Tiếp theo chúng ta bổ sung các hàm vào bên trong lớp này

Page 19: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 19

Tạo menu cho hàm trang edit

Tạo menu cho trang default

Bạn có thể xem ý nghĩa của của từng hàm trong lớp JToolHelper trong bảng bên dưới

Page 20: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 20

Tạo trang default.php trong thư mục views/cauhoi/tmpl

Page 21: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 21

<?php

defined('_JEXEC') or die('Restricted access'); jimport( 'joomla.html.html' );

JHTML::_('behavior.tooltip'); ?>

<form name="adminForm" action="index.php?option=com_hoidap&view=cauhoi" method="post">

<table class="adminlist"> <thead> <th width="20"><input type="checkbox" name="toggle" value=""

onclick="checkAll(<?php echo count( $this->rows ); ?>);" /></th> <th><?php echo JText::_('Câu hỏi'); ?></th>

<th><?php echo JText::_('Đáp án đúng'); ?></th> <th><?php echo JText::_('Người tạo'); ?></th> <th><?php echo JText::_('Ngày tạo'); ?></th>

<th><?php echo JText::_('Trạng thái'); ?></th> <th><?php echo JText::_('ID'); ?></th>

</thead> <?php for($i = 0, $n = count($this->rows);$i < $n; $i++){ $link = JRoute::_( 'index.php?option=com_hoidap&view=cauhoi&task=edit&cid[]='.

$this->rows[$i]->id ); ?>

<tr class="row<?php echo $i%2; ?>"> <td><?php echo JHTML::_('grid.checkedout', $this->rows[$i], $i ) ?></td>

<td> <span class="editlinktip hasTip" title="<?php echo JText::_( 'Chỉnh sửa' );?>:<?php echo htmlspecialchars($this->rows[$i]->title); ?>">

<?php if ( JTable::isCheckedOut(JFactory::getUser()->get('id'), $this->rows[$i]-

>checked_out ) ) { echo htmlspecialchars($this->rows[$i]->title); } else {

?>

<a href="<?php echo $link; ?>"> <?php echo htmlspecialchars($this->rows[$i]->title); ?></a> <?php

} ?>

</span> </td> <td align="center"><?php echo htmlspecialchars(strtoupper($this->rows[$i]-

>dapandung)); ?></td>

<td><?php echo htmlspecialchars($this->rows[$i]->postname); ?></td>

Page 22: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 22

Tạo trang edit.php trong thư mục views/cauhoi/tmpl

<td><?php echo htmlspecialchars($this->rows[$i]->created); ?></td>

<td align="center"><?php echo JHTML::_('grid.published', $this->rows[$i], $i ); ?></td>

<td><?php echo htmlspecialchars($this->rows[$i]->id); ?></td> </tr>

<?php } ?> <tfoot>

<tr> <td colspan="7"><?php echo $this->page->getListFooter(); ?></td> </tr>

</tfoot> </table>

<input type="hidden" name="task" value="" /> <input type="hidden" name="option" value="com_hoidap" /> <input type="hidden" name="boxchecked" value="" />

<input type="hidden" name="c" value="cauhoi" /> <?php echo JHTML::_( 'form.token' ); ?>

</form>

Page 23: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 23

<?php

defined( '_JEXEC' ) or die( 'Restricted access' ); jimport( 'joomla.html.html' );

?> <script language="javascript" type="text/javascript">

function submitbutton(pressbutton) { var form = document.adminForm;

if (pressbutton == 'cancel') { submitform( pressbutton );

return; }

if ( form.title.value == "" ) { alert("<?php echo JText::_( 'Vui lòng nhập tiêu đề câu hỏi', true ); ?>");

}else if ( form.dapandung.value == "" ) { alert("<?php echo JText::_( 'Vui lòng nhập tiêu đề câu hỏi', true ); ?>");

}else{ submitform(pressbutton); }

} </script>

<form name="adminForm" action="index.php?option=com_hoidap&view=cauhoi" method="post">

<div class="col width-60"> <fieldset> <legend><?php echo JText::_('Chi tiết'); ?></legend>

<p></p> <table class="admintable">

<tr> <td class="key" style="width:200px"><?php echo JText::_('Tiêu đề'); ?></td> <td><input type="text" name="title" value="<?php echo $this->row->title; ?>"

size="50" class="inputbox" /></td> </tr>

<tr> <td class="key"><?php echo JText::_('Đáp án đúng (A, B, C hoặc D)'); ?></td> <td><input type="text" name="dapandung" value="<?php echo $this->row-

>dapandung; ?>" size="10" class="inputbox" maxlength="1" /></td> </tr>

<tr> <td class="key"><?php echo JText::_('Trạng Thái'); ?></td> <td><?php echo JHTML::_('select.booleanlist','published','class="inputbox"',$this-

>row->published); ?></td> </tr>

</table> <p></p> </fieldset>

</div>

Page 24: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 24

Thao tác với views đáp án trong thư mục views/dapan - Tạo tập tin view.html.php

<input type="hidden" name="task" value=""/>

<input type="hidden" name="c" value="cauhoi" /> <input type="hidden" name="option" value="com_hoidap"/>

<input type="hidden" name="id" value="<?php echo $this->row->id; ?>"/> <?php echo JHTML::_( 'form.token' ); ?>

</form>

Page 25: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 25

<?php

defined('_JEXEC') or die('Restricted access'); jimport('joomla.application.component.view');

class HoidapViewDapan extends JView { function display($tpl = null)

{ jimport('joomla.html.pagination');

$db = & JFactory::getDBO(); global $mainframe;

$model = $this->getModel(); $limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit',

$mainframe->getCfg('list_limit'), 'int' ); $limitstart = $mainframe->getUserStateFromRequest( 'cauhoi.limitstart', 'limitstart', 0, 'int' );

$filter_order = $mainframe->getUserStateFromRequest( 'cauhoi.filter_order', 'filter_order', 'd.id', 'cmd' );

$filter_order_Dir = $mainframe->getUserStateFromRequest( 'cauhoi.filter_order_Dir', 'filter_order_Dir', '', 'word' ); $filter_state = $mainframe->getUserStateFromRequest( 'cauhoi.filter_state',

'filter_state', '', 'word' ); $search = $mainframe->getUserStateFromRequest( 'cauhoi.search', 'search', '', 'string' );

$filter_cauhoi = JRequest::getVar('filter_cauhoi', '', 'DEFAULT','string' );

if (strpos($search, '"') !== false) { $search = str_replace(array('=', '<'), '', $search);

}

$search = JString::strtolower($search); $where = array();

if ( $filter_state )

{ if ( $filter_state == 'P' ) { $where[] = 'd.published = 1';

} else if ($filter_state == 'U' ) {

$where[] = 'd.published = 0'; }

}

Page 26: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 26

if ($filter_cauhoi) {

$where[] = 'd.idcauhoi = ' .$db->Quote($filter_cauhoi); }

if ($search) { $where[] = 'LOWER(d.title) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true

).'%', false ); }

$where = count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '';

$list = array();

$list['search'] = $search; $list['state'] = JHTML::_('grid.state', $filter_state );

$javascript = 'onchange="document.adminForm.submit();"'; $list['cauhoi'] = $model->cauhoiRef( 'filter_cauhoi',$filter_cauhoi, $javascript);

$list['order_Dir'] = $filter_order_Dir; $list['order'] = $filter_order;

$this->assignRef('list', $list);

$total = $model->getTotal(); $pageNav = new JPagination( $total, $limitstart, $limit );

$this->assignRef('page', $pageNav); $task = JRequest::getCmd('task');

switch($task) {

case 'add': case 'edit': $this->_EDITTOOLBAR();

$this->setLayout('edit'); $row = $model->editDapan();

$this->assignRef('row', $row); $this->assignRef('cauhoi_list', $model->getCauhoi($row->idcauhoi)); break;

default: $this->_DEFAULTTOOLBAR();

$this->assignRef('rows', $model->showDapan($where, $limitstart, $limit)); break; }

parent::display($tpl);

}

Page 27: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 27

- Tạo trang edit.php nằm trong thư mục views/dapan/tmpl

<?php defined('_JEXEC') or die('Restricted access'); ?>

<form name="adminForm" action="index.php?option=com_hoidap&view=dapan&c=dapan" method="post">

<fieldset> <legend><?php echo JText::_('Thông tin chi tiết'); ?></legend> <table class="admintable">

<tr> <td class="key"><?php echo JText::_('Tiêu đề'); ?></td>

<td><input type="text" name="title" value="<?php echo @$this->row->title; ?>" size="80" style="width:400px" /></td> </tr>

<tr> <td class="key"><?php echo JText::_('Câu hỏi'); ?></td>

<td><?php echo $this->cauhoi_list; ?></td> </tr> <tr>

<td class="key"><?php echo JText::_('Thứ tự (A, B, C hoặc D)'); ?></td> <td><input type="text" name="thutudapan" value="<?php echo @$this->row-

>thutudapan; ?>" size="5" style="width:100px" maxlength="1" /></td>

</tr>

function _EDITTOOLBAR()

{ JToolBarHelper::title('Chỉnh sửa');

JToolBarHelper::save(); JToolBarHelper::apply(); JToolBarHelper::cancel();

}

function _DEFAULTTOOLBAR() { JToolBarHelper::title('Đáp án');

JToolBarHelper::addNewX(); JToolBarHelper::editListX();

JToolBarHelper::deleteListX(); JToolBarHelper::publishList(); JToolBarHelper::unpublishList();

} }

?>

Page 28: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 28

- Tạo trang default.php trong thư mục views/dapan/tmpl

<?php defined('_JEXEC') or die('Restricted access'); jimport( 'joomla.html.html' );

JHTML::_('behavior.tooltip'); ?>

<form name="adminForm" action="index.php?option=com_hoidap&view=dapan&c=dapan" method="post"> <table>

<tr> <td align="left" width="100%">

<?php echo JText::_( 'Đáp án' ); ?>: <input type="text" name="search" size="30" id="search" value="<?php echo htmlspecialchars($this->list['search']);?>" class="text_area"

onchange="document.adminForm.submit();" /> <button onclick="this.form.submit();"><?php echo JText::_( 'Tìm kiếm' );

?></button> <button onclick="document.getElementById('search').value='';this.form.getElementById('filter_ state').value=

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

</td> <td nowrap="nowrap"> <?php

echo @$this->list['cauhoi'].'&nbsp;'; echo @$this->list['state'];

?> </td> </tr>

</table>

<tr>

<td class="key"><?php echo JText::_('Trạng Thái'); ?></td> <td><?php echo JHTML::_('select.booleanlist','published','class="inputbox"',$this->row-

>published); ?></td> </tr> </table>

</fieldset> <input type="hidden" name="task" value=""/>

<input type="hidden" name="c" value="dapan" /> <input type="hidden" name="option" value="com_hoidap"/> <input type="hidden" name="id" value="<?php echo $this->row->id; ?>"/>

<?php echo JHTML::_( 'form.token' ); ?>

</form>

Page 29: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 29

<table class="adminlist">

<thead> <th width="20"><input type="checkbox" name="toggle" value=""

onclick="checkAll(<?php echo count( $this->rows ); ?>);" /></th> <th width="120"><?php echo JText::_('Thứ tự câu hỏi'); ?></th> <th><?php echo JText::_('Trả lời'); ?></th>

<th><?php echo JText::_('Câu hỏi'); ?></th> <th><?php echo JText::_('Người tạo'); ?></th>

<th><?php echo JText::_('Ngày tạo'); ?></th> <th><?php echo JText::_('Trạng thái'); ?></th> <th><?php echo JText::_('ID'); ?></th>

</thead> <?php for($i = 0, $n = count($this->rows);$i < $n; $i++){

$link = JRoute::_( 'index.php?option=com_hoidap&view=dapan&c=dapan&task=edit&cid[]='. $this->rows[$i]->id ); ?>

<tr class="row<?php echo $i%2; ?>">

<td><?php echo JHTML::_('grid.checkedout', $this->rows[$i], $i ) ?></td> <td align="center"><?php echo htmlspecialchars(strtoupper($this->rows[$i]->thutudapan)); ?></td>

<td> <span class="editlinktip hasTip" title="<?php echo JText::_( 'Chỉnh sửa' );?>:<?php echo htmlspecialchars($this->rows[$i]->title); ?>">

<?php if ( JTable::isCheckedOut(JFactory::getUser()->get('id'), $this->rows[$i]-

>checked_out ) ) { echo htmlspecialchars($this->rows[$i]->title); } else {

?>

<a href="<?php echo $link; ?>"> <?php echo htmlspecialchars($this->rows[$i]->title); ?></a> <?php

} ?>

</span> </td> <td><?php echo htmlspecialchars($this->rows[$i]->cauhoi); ?></td>

<td><?php echo htmlspecialchars($this->rows[$i]->postname); ?></td> <td><?php echo htmlspecialchars($this->rows[$i]->created); ?></td>

<td align="center"><?php echo JHTML::_('grid.published', $this->rows[$i], $i ); ?></td> <td><?php echo htmlspecialchars($this->rows[$i]->id); ?></td> </tr>

<?php } ?>

Page 30: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 30

1.7 THAO TÁC VỚI CONTROLLER

Tạo controller cho views câu hỏi, trong thư mục controllers/ tạo 1 tập tin tên cauhoi.php với nội dung như sau:

<?php defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller'); class HoidapControllerCauhoi extends JController

{ function __construct($config = array()) { parent::__construct($config);

$this->registerTask('apply', 'save'); $this->registerTask('unpublish', 'publish');

} function cancel()

{ JRequest::checkToken() or jexit( 'Invalid Token' );

$table = JTable::getInstance('cauhoi'); $table->bind(JRequest::get('post')); $table->checkin($table->get('id'));

$this->setRedirect('index.php?option=com_hoidap&view=cauhoi'); }

<tfoot>

<tr> <td colspan="8"><?php echo $this->page->getListFooter(); ?></td>

</tr> </tfoot> </table>

<input type="hidden" name="task" value="" /> <input type="hidden" name="option" value="com_hoidap" />

<input type="hidden" name="boxchecked" value="" /> <input type="hidden" name="c" value="dapan" /> <input type="hidden" name="filter_order" value="<?php echo $this->list['order']; ?>" />

<input type="hidden" name="filter_order_Dir" value="<?php echo $this->list['order_Dir']; ?>" /> <?php echo JHTML::_( 'form.token' ); ?>

</form>

Page 31: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 31

function save()

{ JRequest::checkToken() or jexit( 'Invalid Token' );

$table = JTable::getInstance('cauhoi'); if(!$table->bind(JRequest::get('post'))){

// Xu ly loi return JError::raisewarning('','Đã xảy ra lỗi');

} if(!$table->store()){

return JError::raisewarning('','Đã xảy ra lỗi trong quá trình lưu lại'); }

if($this->_task == 'apply'){ $this->setRedirect('index.php?option=com_hoidap&view=cauhoi&task=edit&cid[]='.$table-

>get('id'), 'Nội dung đã được luư lại'); }else{

$table->checkin($table->get('id')); $this->setRedirect('index.php?option=com_hoidap&view=cauhoi', 'Đã thêm 1 tập tin'); }

} function publish()

{ JRequest::checkToken() or jexit( 'Invalid Token' );

$table = JTable::getInstance('cauhoi'); $cid = JRequest::getVar('cid',NULL,'','array'); $status = ($this->_task == 'publish') ? 1 : 0;

$table->publish($cid, $status); $this->setRedirect('index.php?option=com_hoidap&view=cauhoi');

}

Page 32: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 32

Tiếp theo tạo tập tin dapan.php trong thư mục contollers/dapan.php. Nội dung của tập tin này như sau:

function remove()

{ JRequest::checkToken() or jexit('Invalid Token');

$db = JFactory::getDBO(); $table = JTable::getInstance('cauhoi'); $noteDelete = 0;

// Kiem tra su ton tai trong muc dap an $cid = JRequest::getVar('cid',NULL,'DEFAULT','array');

if(is_null($cid)){ return JError::raisewarning('','Lỗi ! Chưa có id nào được chọn !');

}

foreach($cid as $id) { $query = "SELECT COUNT(*) FROM #__hoidap_dapan WHERE idcauhoi = ".$db-

>Quote($id);

$db->setQuery($query); if($db->loadResult() == 0){ $table->delete($id);

}else{ $noteDelete++; }

}

if($noteDelete){ $this->setRedirect('index.php?option=com_hoidap&view=cauhoi','Chú ý: Có những mẫu tin không thể xóa do còn tồn tại mẫu tin con !');

}else{ $this->setRedirect('index.php?option=com_hoidap&view=cauhoi','Đã xóa thành công các

mẫu tin đã chọn !'); } }

}

?>

Page 33: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 33

<?php

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller'); class HoidapControllerDapan extends JController {

function __construct($config = array()) { parent::__construct($config);

$this->registerTask('unpublish', 'publish'); $this->registerTask('apply', 'save'); }

function remove()

{ JRequest::checkToken() or jexit('Invalid Token'); $noteDelete = 0;

$table = JTable::getInstance('dapan'); $cid = JRequest::getVar('cid',array(0),'DEFAULT','array');

JArrayHelper::toInteger($cid); if(empty($cid)){

return JError::rasiewarning('','Lỗi ! Bạn chưa chọn tập tin để xóa '); }

foreach($cid as $id){ if(!$table->delete($id)){

$noteDelete++; } }

if($noteDelete){

$this->setRedirect('index.php?option=com_hoidap&view=dapan&c=dapdan', 'Không thể xóa '.$noteDelete.' mẫu tin'); }else{

$this->setRedirect('index.php?option=com_hoidap&view=dapan&c=dapdan','Đã xóa thành công !');

}

}

Page 34: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 34

function cancel()

{ JRequest::checkToken() or jexit('Invalid Token');

$table = JTable::getInstance('dapan'); if(!$table->bind(JRequest::get('post'))){

// Lỗi nè }

$table->checkin($table->get('id')); $this->setRedirect('index.php?option=com_hoidap&view=dapan&c=dapan'); }

function save()

{ JRequest::checkToken() or jexit( 'Invalid Token' ); $table = JTable::getInstance('dapan');

if(!$table->bind(JRequest::get('post'))){

// Xu ly loi return JError::raisewarning('','Đã xảy ra lỗi'); }

if(!$table->store()){ return JError::raisewarning('','Đã xảy ra lỗi trong quá trình lưu lại');

}

if($this->_task == 'apply'){ $this->setRedirect('index.php?option=com_hoidap&view=dapan&c=dapan&task=edit&cid[]='.$table-

>get('id'), 'Nội dung đã được luư lại'); }else{

$table->checkin($table->get('id')); $this->setRedirect('index.php?option=com_hoidap&view=dapan&c=dapan', 'Nội dung đã được lưu lại');

} }

Page 35: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 35

2. XÂY DỰNG COMPONENT BÊN NGOÀI (FRONT-END)

2.1 CẤU TRÚC CỦA COMPONENT FRONT-END

2.2 XÂY DỰNG COMPONENT

2.2.1 XÂY DỰNG ĐIỂM VÀO CHO COMPONENT

- Khác với component trong quản trị, tên điểm vào chính của component bên ngoài Front-end có tên là <Tên component (bỏ phần tiếp dầu ngữ )>.php, ở đây điểm vào của chúng ta là hoidap.php

Nội dung của tập tin hoidap.php như sau:

function publish()

{ JRequest::checkToken() or jexit( 'Invalid Token' );

$table = JTable::getInstance('dapan'); $cid = JRequest::getVar('cid',NULL,'','array'); $status = ($this->_task == 'publish') ? 1 : 0;

$table->publish($cid, $status); $this->setRedirect('index.php?option=com_hoidap&view=dapan&c=dapan');

} }

?>

Page 36: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 36

Tại thư mục com_hoidap/models/ tạo tập tin tracnghiem.php có nội dung như sau:

Tai thư mục com_hoidap/views /tracnghiem tạo tập tin view.html.php

<?php defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class HoidapModelTracnghiem extends JModel { function getCauhoi()

{ $query = " SELECT * FROM ".$this->_db->nameQuote('#__hoidap_cauhoi')." WHERE

published = 1"; return $this->_getList($query); }

}

?>

<?php

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.filter.filteroutput' ); // Require controller

require_once( JPATH_COMPONENT.DS.'controller.php' );

// Create the controller $controller = new HoidapControllerTracnghiem();

// Perform the Request task $controller->execute( JRequest::getCmd('task') );

// Redirect if set by the controller $controller->redirect();

?>

Page 37: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 37

Trong thư mục com_hoidap/views/tracnghiem/ tạo mới tập tin tên default.php có nội dung như sau

<?php

defined('_JEXEC') or die('Restricted access'); jimport('joomla.application.component.view');

class HoidapViewTracnghiem extends JView {

function display($tpl = null) { $model = $this->getModel();

$this->assignRef('rows', $model->getCauhoi()); parent::display($tpl);

}

}

?>

Page 38: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 38

Controller.php

<?php defined('_JEXEC') or die('Restricted access'); ?>

<form action="index.php?option=com_hoidap&view=tracnghiem" method="post"

name="clientForm" onsubmit="return checkValue()"> <?php $db = & JFactory::getDBO();

echo '<ol>'; for($i = 0; $i <count($this->rows); $i++ )

{ echo '<input type="hidden" name="idcauhoi[]" value='.$this->rows[$i]->id.' />';

$query = " SELECT * FROM ".$db->nameQuote('#__hoidap_dapan') ." WHERE idcauhoi = ".$db->Quote((int)$this->rows[$i]->id)

." AND published = 1 ORDER BY thutudapan"; $db->setQuery($query); $rowsdapan = $db->loadObjectList();

?> <li><?php echo $this->rows[$i]->title; ?></li>

<?php echo '<ul style="list-style:none; padding:10px 0 10px 0;">';

for($j = 0; $j < count($rowsdapan); $j++ ) { ?>

<li><input type="radio" name="<?php echo 'cauhoi'.$i; ?>" value="<?php echo strtoupper($rowsdapan[$j]->thutudapan); ?>"/><?php echo $rowsdapan[$j]->title; ?></li>

<?php } echo '</ul>';

}

echo '</ol>'; ?> <input type="hidden" name="task" value="complete" />

<input type="submit" name="submit" value="Submit" /> <?php echo JHTML::_( 'form.token' ); ?>

</form>

Page 39: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 39

<?php

defined('_JEXEC') or die('Restricted access'); jimport('joomla.application.component.controller');

class HoidapController extends JController { function complete()

{ JRequest::checkToken() or die('Form khong hop le');

$fail = array(); $db = & JFactory::getDBO(); $idcauhoi = JRequest::getVar('idcauhoi',NULL,'POST','array');

$count = 0; for($i = 0; $i < count($idcauhoi); $i++)

{ $dapan = JRequest::getVar("cauhoi$i",'','POST','string');

$sql = "SELECT dapandung FROM ".$db->nameQuote('#__hoidap_cauhoi')." WHERE id = ".$db->Quote($idcauhoi[$i]);

$db->setQuery($sql); $row = $db->loadObjectList();

($dapan == $row[$i]->dapandung) ? ($count++) : ($fail[] = $idcauhoi[$i]);

}

if(!empty($fail)){

echo 'Cau sai: '; foreach ($fail as $data)

{ echo $data."<br />"; }

}

if($count){ echo 'Ban tra loi dung '.$count; }else{

echo 'Ban tra loi sai toan bo '; }

unset($fail); }

}

?>

Page 40: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 40

3. DEMO COMPONENT

1. Kết quả trong quản trị

1.1 View câu hỏi

- Trang mặc định index.php?option=com_hoidap&view=cauhoi

- Trang sửa một tập 1 câu hỏi index.php?option=com_hoidap&view=cauhoi&task=edit&cid[]=1

- Trang thêm mới câu hỏi

1.2 View đáp án

- Trang default.php tại địa chỉ index.php?option=com_hoidap&view=dapan&c=dapan

Page 41: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 41

- Trang edit.php tại địa chỉ (để sửa mẫu tin có id=1)

index.php?option=com_hoidap&view=dapan&c=dapan&task=edit&cid[]=1

Thêm mới đáp án

2. Kết quả bên ngoài FRONT-END

Page 42: Lap trinh-joomla-15-theo-mo-hinh-mvc

JoomlaBasic.Com Page 42

Kết quả sau khi đánh giá