hướng dẫn viết module cho phiên bản nukeviet 3

72
Hướng dn viết module cho phiên bn NukeViet 3.0 October 2, 2010 BI xman 5 Comments HƯỚNG DN VIT MODULE CHO PHIÊN BN NUKEVIET CMS 3.0 CÁC BÀI VIT: 1. Gii thiu vcu trúc cơ bn ca module 2. Cách viết mt module đơn gin 3. Thêm link qun lý module vào menu qun tr4. Thêm 1 submenu qun lý module trong admin 5. Kết ni vi file ngôn ng6. Kết ni vi cơ sdliu 7. Kết ni vi template engine (Xtemplate) 8. Các chú ý khi lp trình 1. Cu trúc cơ bn khi bt đầu viết module Khác vi nhng phiên bn trước đây ca hthng nukeviet, trong phiên bn mi này vic đóng gói module được ti ưu, các file và folder ca module đều được đưa vào trong 1 thư mc. Mt module example có cơ sdliu và có phn qun trcho người dùng có cu trúc căn bn như sau: admin —-.htaccess —-main.php modules funcs —-index.html —-.htaccess —-main.php language —-index.html —-.htaccess —-vi.php —-vi_admin.php js —-.htaccess —-javascript.js action.php admin.functions.php functions.php version.php

Upload: pham-cong-thien

Post on 24-Apr-2015

438 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hướng dẫn viết module cho phiên bản NukeViet 3

Hướng dẫn viết module cho phiên bản NukeViet 3.0 October 2, 2010 BỞI xman 5 Comments

HƯỚNG DẪN VIẾT MODULE CHO PHIÊN BẢN NUKEVIET CMS 3.0

CÁC BÀI VIẾT:

1. Giới thiệu về cấu trúc cơ bản của module 2. Cách viết một module đơn giản 3. Thêm link quản lý module vào menu quản trị 4. Thêm 1 submenu quản lý module trong admin 5. Kết nối với file ngôn ngữ 6. Kết nối với cơ sở dữ liệu 7. Kết nối với template engine (Xtemplate) 8. Các chú ý khi lập trình

1. Cấu trúc cơ bản khi bắt đầu viết module Khác với những phiên bản trước đây của hệ thống nukeviet, trong phiên bản mới này việc đóng gói module được tối ưu, các file và folder của module đều được đưa vào trong 1 thư mục. Một module example có cơ sở dữ liệu và có phần quản trị cho người dùng có cấu trúc căn bản như sau:

admin —-.htaccess —-main.php modules funcs —-index.html —-.htaccess —-main.php language —-index.html —-.htaccess —-vi.php —-vi_admin.php js —-.htaccess —-javascript.js action.php admin.functions.php functions.php version.php

Page 2: Hướng dẫn viết module cho phiên bản NukeViet 3

index.html .htaccess

2. Cách viết một module đơn giản

Chú ý: Tên module mới chỉ gồm các chữ cái, số và dấu gạch ngang, bắt buộc phải bắt đầu bằng một chữ cái. - Trong bài viết này chúng ta xây dựng một module đơn giản để xuất ra nội dung,: Hello world!!! - Các bước chuẩn bị: Tạo các file và folder như sau:

$ modules/example/index.html $ modules/example/funcs/main.php $ modules/example/funcs/index.html $ modules/example/admin.functions.php $ modules/example/functions.php $ modules/example/theme.php $ modules/example/version.php

modules/example/functions.php

view source print? 1 <?php 2 3 if (!defined('NV_SYSTEM')) 4 die('Stop!!!');

5 6 define('NV_IS_MOD_EXAMPLE', true);7 ?>

modules/example/version.php

view source print? 01 <?php 02

03 if ( ! defined( 'NV_ADMIN' ) or ! defined( 'NV_MAINFILE' )) die( 'Stop!!!' ); 04 05 $module_version = array( 06 "name" => "Example", //

07 "modfuncs" => "main", // "is_sysmod" => 0, // "virtual" => 0, // "version" => "3.0.01", //

Page 3: Hướng dẫn viết module cho phiên bản NukeViet 3

08 "date" => "2 Oct 2010 09:47:15 GMT", // "author" => "http://www.thegioimanguon.com", // "note" => ""

09 ); 10 11 ?>

Các chú ý khi khai báo trong file version.php - “modfuncs” => “main”, là danh sách các các function tương ứng với tên các file trong folder funcs của module. - “virtual” => 0: Module không được ảo hóa - “virtual”=>1: Module được ảo hóa. modules/example/funcs/main.php

view source print? 1 <?php 2 3 if ( ! defined( 'NV_IS_MOD_EXAMPLE' ) ) die( 'Stop!!!' );4 $contents = "Hello world"; 5 include ( NV_ROOTDIR . "/includes/header.php" );6 echo nv_site_theme( $contents );7 include ( NV_ROOTDIR . "/includes/footer.php" );8 ?>

Sau khi thực hiện các bước như trên ta có được 1 module đơn giản, Để xem kết quả bạn đăng nhập vào khu vực quản trị với quyền admin, cài đặt và click hoạt module để có thể xem module qua link:

http://mydomain.com/index.php?lang=vi&nv=example

Nếu bạn muốn thêm 1 func mới cho module bạn tạo mới 1 file (new.php) trong thư mục: modules/example/funcs/

Sau đó bạn cần đăng nhập và khu vực quản trị kích hoạt các function này.

3. Thêm link quản lý module vào menu quản trị modules/example/admin/functions.php

view source print? 1 <?php 2

3 if (! defined('NV_ADMIN') or ! defined('NV_MAINFILE') or ! defined('NV_IS_MODADMIN')

4 ) 5 die('Stop!!!');

Page 4: Hướng dẫn viết module cho phiên bản NukeViet 3

6 $allow_func = array('main');7 define( 'NV_IS_EXAMPLE_ADMIN', true );8 9 ?>

modules/example/admin/main.php

view source print? 01 <?php 02 03 if ( ! defined( 'NV_IS_EXAMPLE_ADMIN' ) )04 { 05 die( 'Stop!!!' ); 06 } 07 $page_title = "main"; 08 $contents = "test content";

09 10 include ( NV_ROOTDIR . "/includes/header.php" );11 echo nv_admin_theme( $contents );12 include ( NV_ROOTDIR . "/includes/footer.php" );13 ?>

Để có thể quản trị module thì trong thư mục admin bắt buộc phải chứa file: main.php và trong thư mục tương ứng với các file functions.php, version.php bắt buộc phải chứa file: admin.functions.php

Chú ý: Để các function có thể hoạt động được thì function phải được gán vào mảng $allow_func

Khi đó danh mục các file của module gồm:

$ modules/example/admin/index.html $ modules/example/admin/functions.php $ modules/example/admin/main.php $ modules/example/index.html $ modules/example/funcs/main.php $ modules/example/funcs/index.html $ modules/example/functions.php $ modules/example/version.php

4. Thêm 1 submenu quản lý module trong admin Thay đổi lại file modules/example/admin.functions.php

view source

Page 5: Hướng dẫn viết module cho phiên bản NukeViet 3

print? 01 <?php 02

03 if ( ! defined( 'NV_ADMIN' ) or ! defined( 'NV_MAINFILE' ) or ! defined( 'NV_IS_MODA DMIN' ) ) die( 'Stop!!!' ); 04 $allow_func = array('main');05 $submenu['add'] = "add"; 06 $submenu['edit'] = "edit"; 07 $allow_func = array('main', 'add', 'edit');08 09 define( 'NV_IS_EXAMPLE_ADMIN', true );10 11 ?>

5. Kết nối với file ngôn ngữ Sửa lại nội dung file modules/example/admin.functions.php

view source print? 01 <?php 02

03 if ( ! defined( 'NV_ADMIN' ) or ! defined( 'NV_MAINFILE' ) or ! defined( 'NV_IS_MODA DMIN' ) ) die( 'Stop!!!' );

04 $allow_func = array('main');05 $submenu['add'] = $lang_module['example_add'];06 $submenu['edit'] = $lang_module['example_edit'];07 $allow_func = array('main', 'add', 'edit');08 09 define( 'NV_IS_EXAMPLE_ADMIN', true );10 11 ?>

Quy tắc đặt tên file ngôn ngữ: Các file ngôn ngữ được đặt trong thư mục language của module, Nếu ngôn ngữ cho module sẽ đặt tên: vi.php, en.php, ngôn ngữ cho admin admin_vi.php, admin_en.php

Ta tạo file ngôn ngữ tiếng Việt cho module example: modules/example/language/ admin_vi.php

view source print? 01 <?php 02

Page 6: Hướng dẫn viết module cho phiên bản NukeViet 3

03 if (! defined('NV_ADMIN') or ! defined('NV_MAINFILE')){04 die('Stop!!!'); 05 } 06 07 $lang_translator['author'] ="http://www.thegioimanguon.com"; 08 $lang_translator['createdate'] ="04/03/2010, 15:22";09 $lang_translator['copyright'] ="";10 $lang_translator['info'] ="";11 $lang_translator['langtype'] ="lang_module";12 13 $lang_module['example_add']= "Thêm";14 $lang_module['example_edit']= "Sửa";

15 16 ?>

Khi đó danh mục các file của module gồm:

$ modules/example/admin/index.html $ modules/example/admin/main.php $ modules/example/index.html $ modules/example/funcs/main.php $ modules/example/funcs/index.html $ modules/example/admin.functions.php $ modules/example/functions.php $ modules/example/theme.php $ modules/example/version.php $ modules/example/language/vi_admin.php $ modules/example/language/en_admin.php

6. Kết nối với cơ sở dữ liệu

modules/example/action.php

view source print? 01 <?php 02 03 if ( ! defined( 'NV_IS_FILE_MODULES' ) ) die( 'Stop!!!' ); 04 05 $sql_drop_module = array();06

07 $sql_drop_module[] = "DROP TABLE IF EXISTS " . $db_config['prefix'] . "_" .$lang . "_" . $module_data . "`;";

Page 7: Hướng dẫn viết module cho phiên bản NukeViet 3

08 09 $sql_create_module = $sql_drop_module;10

11 $sql_create_module[] = "CREATE TABLE " . $db_config['prefix'] . "_" . $lang . "_" . $module_data . "` (

12 `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,13 `title` varchar(255) NOT NULL, PRIMARY KEY (`id`)14 ) ENGINE=MyISAM DEFAULT CHARSET=utf8";15 ?>

Sau khi tạo xong file action.php, bạn vào phần quản lý module để cài lại module example. Khi đó cơ sở dữ liệu sẽ được tạo tự động.

Sửa lại file modules/example/admin/add.php

view source print? 01 <?php 02 03 if ( ! defined( 'NV_IS_EXAMPLE_ADMIN' ) )04 { 05 die( 'Stop!!!' ); 06 } 07 $page_title = $lang_module['example_add'];08 $add = 0; 09 $error = ""; 10 11 if ( $nv_Request->get_int( 'add', 'post' ) == 1 )12 { 13 $title = filter_text_input( 'title', 'post', '', 1 );14 if ( empty( $title ) ) 15 { 16 $error = $lang_module['err_title'];17 } 18 else 19 { 20

21 $query = "INSERT INTO `" . NV_PREFIXLANG . "_" . $module_data . "` (`id`, `title`) VALUES (NULL, " . $db->dbescape( $title ) . ")";

22 if ( $db->sql_query_insert_id( $query ) )23 { 24 $db->sql_freeresult();

Page 8: Hướng dẫn viết module cho phiên bản NukeViet 3

25 Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABL E . "=" . $module_name . "" ); 26 die();

27 28 } 29 else 30 {

31 32 } 33 } 34 }

35 36 $error = $lang_module['errorsave'];

37 38 if ( $error != "" ) 39 { 40 $contents .= "<div class=\"quote\" style=\"width:780px;\">\n";

41 $contents .= "<blockquote class=\"error\"><span>" . $error . "</span></blockquote>\n";

42 $contents .= "</div>\n"; 43 $contents .= "<div class=\"clear\"></div>\n";44 }

45 $contents .= "<form action=\"" . NV_BASE_ADMINURL . "index.php\" method=\"POST\">\n";

46 $contents .= "<table class=\"tab1\">\n";47 $contents .= " <tr>\n";

48 $contents .= " <td width=\"30%\"><strong>" . $lang_module['title'] . "<strong></td>\n";

49 $contents .= " <td>\n"; 50 $contents .= " <input type=\"text\" name=\"title\" size=\"100\">\n";51 $contents .= " </td>\n"; 52 $contents .= " </tr>\n"; 53 $contents .= "</table>\n"; 54 $contents .= "<table class=\"tab2\" align=\"center\">\n"; 55 $contents .= " <tr>\n"; 56 $contents .= " <td style=\"text-align:center;\">\n";

57 $contents .= " <input type=\"submit\" value=\"" . $lang_module['save'] . "\"/>\n";

58 $contents .= " <input name=\"add\" type=\"hidden\" value=\"1\" />\n";

59 $contents .= " <input type=\"hidden\" name =\"" . NV_NAME_VARIABLE . "\"value=\"" . $module_name . "\" />";

60 $contents .= " <input type=\"hidden\" name =\"" . NV_OP_VARIABLE . "\ "value=\"" . $op . "\" />"; 61 $contents .= " </td>\n";

Page 9: Hướng dẫn viết module cho phiên bản NukeViet 3

62 $contents .= " </tr>\n"; 63 $contents .= "</table>\n"; 64 $contents .= "</form>\n";

65 66 include ( NV_ROOTDIR . "/includes/header.php" );67 echo nv_admin_theme( $contents );68 include ( NV_ROOTDIR . "/includes/footer.php" );

69 70 ?>

Sửa lại file modules/example/language/vi_admin.php

view source print? 01 <?php 02 03 if (! defined('NV_ADMIN') or ! defined('NV_MAINFILE')){04 die('Stop!!!'); 05 } 06 07 $lang_translator['author'] ="http://www.thegioimanguon.com"; 08 $lang_translator['createdate'] ="04/03/2010, 15:22";09 $lang_translator['copyright'] ="";10 $lang_translator['info'] ="";11 $lang_translator['langtype'] ="lang_module";12 13 $lang_module['list']= "Danh sách bài viết";14 $lang_module['example_add']= "Thêm mới bài viết";15 $lang_module['example_edit']= "Sửa bài viết";16 $lang_module['example_del']= "Xóa bài viết";17 $lang_module['id']= "ID"; 18 $lang_module['title']= "Tiêu đề";19 $lang_module['func']= "Chức năng";20 $lang_module['save'] = "Lưu";21 $lang_module['err_title'] = "Bạn phải nhập tên";

22 $lang_module['errorsave'] = "Vì một số lý do nào đó mà tên không được lưu vào cơ sở dữ liệu";

23 24 ?>

Sửa lại file modules/example/admin/main.php

Page 10: Hướng dẫn viết module cho phiên bản NukeViet 3

view source print? 01 <?php 02 03 if ( ! defined( 'NV_IS_EXAMPLE_ADMIN' ) ) {04 die( 'Stop!!!' ); 05 } 06 $page_title = $lang_module['list'];

07 08 $contents = ""; 09 $contents .= "<table class=\"tab1\">\n";10 $contents .= "<thead>"; 11 $contents .= "<tr style=\"text-align:center;\">\n";12 $contents .= "<td>" . $lang_module['id'] . "</td>\n";13 $contents .= "<td>" . $lang_module['title'] . "</td>\n";14 $contents .= "<td>" . $lang_module['func'] . "</td>\n";15 $contents .= "</tr>\n"; 16 $a = 0;

17 $sql = "SELECT id, title FROM `" . NV_PREFIXLANG . "_" . $module_data . "`"; 18 $resuilt = $db->sql_query( $sql );19 while ( $row = $db->sql_fetchrow( $resuilt ) )20 { 21 $class = ( $a % 2 == 0 ) ? "" : " class=\"second\"";22 $contents .= "<tbody" . $class . ">";23 $contents .= "<tr>\n"; 24 $contents .= "<td style=\"text-align:center;\">" . $row['id'] . "</td>\n";25 $contents .= "<td>" . $row['title'] . "</td>\n";26 $contents .= "<td style=\"text-align:center;\">

27 <a href=\"index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&" . NV_OP_ VARIABLE . "=edit&amp;id=" . $row['id'] . "\">" . $lang_module['example_edit'] . "</ a> -

28 <a href=\"index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&" . NV_OP_VARIABLE . "=del&amp;id=" . $row['id'] . "\">" . $lang_module['example_del'] . "</a>

29 </td>\n"; 30 $contents .= "</tr>\n"; 31 $contents .= "</tbody>\n"; 32 $a++; 33 } 34 $contents .= "</table>\n"; 35 include ( NV_ROOTDIR . "/includes/header.php" );36 echo nv_admin_theme( $contents );37 include ( NV_ROOTDIR . "/includes/footer.php" );

Page 11: Hướng dẫn viết module cho phiên bản NukeViet 3

38 ?>

Sửa lại file modules/example/funcs/main.php

view source print? 01 <?php 02 03 if ( ! defined( 'NV_IS_MOD_EXAMPLE' ) ) die( 'Stop!!!' );

04 $sql = "SELECT `id`, `title` FROM `" . NV_PREFIXLANG . "_" . $module_data . "`";

05 $result = $db->sql_query( $sql );06 $contents = "<table>\n"; 07 $a = 1; 08 while ( $row = $db->sql_fetchrow( $result ) )09 { 10 $contents .= "<tr>\n"; 11 $contents .= "<td>" . $a . "</td><td>" . $row['title'] . "</td></tr>\n";12 $contents .= "</tr>\n"; 13 $a ++; 14 } 15 $contents .= "</table>\n"; 16 include ( NV_ROOTDIR . "/includes/header.php" );17 echo nv_site_theme( $contents );18 include ( NV_ROOTDIR . "/includes/footer.php" );19 ?>

Danh mục các file:

$ modules/example/admin/index.html $ modules/example/admin/main.php $ modules/example/index.html $ modules/example/funcs/main.php $ modules/example/funcs/index.html $ modules/example/admin.functions.php $ modules/example/functions.php $ modules/example/action.php $ modules/example/language/vi_admin.php $ modules/example/language/en_admin.php

7. Kết nối với template engine (Xtemplate)

Bắt đầu từ phiên bản nukeviet 3.0, hệ thống template được tách biệt hẳn ra với mã php. Hiện tại, hệ thống template của nukeviet đang sử dụng Xtemplate để xử lý cho việc tách biệt mã php và

Page 12: Hướng dẫn viết module cho phiên bản NukeViet 3

html. Để tìm hiểu thêm về cách viết Xtemplate, các bạn có thể đọc thêm tài liệu về tại địa chỉ: http://sourceforge.net/projects/xtpl/files/XTemplate%20PHP5/ Sơ đồ template admin control panel sử dụng Xtemplate

themes —-admin_default css —-example.css images js modules —-example.php tpl —-example.tpl

Để sử dụng Xtemplate cho admin control panel của module Example ta tiến hành các bước như sau: Sửa lại file modules/example/admin/main.php

view source print? 01 <?php 02 03 if ( ! defined( 'NV_IS_EXAMPLE_ADMIN' ) ) {04 die( 'Stop!!!' ); 05 } 06 $page_title = $lang_module['list'];

07 $sql = "SELECT id, title FROM `" . NV_PREFIXLANG . "_" . $module_data . "`"; 08 $resuilt = $db->sql_query( $sql );09 while ( $row = $db->sql_fetchrow( $resuilt ) )10 { 11 $content[] = $row; 12 }

13 $contents = call_user_func("main", $content); include ( NV_ROOTDIR . "/includes/header.php" ); echo nv_admin_theme( $contents );

14 include ( NV_ROOTDIR . "/includes/footer.php" );15 ?>

Sửa lại file modules/example/admin/add.php

view source print? 01 <?php 02

Page 13: Hướng dẫn viết module cho phiên bản NukeViet 3

03 if ( ! defined( 'NV_IS_EXAMPLE_ADMIN' ) )04 { 05 die( 'Stop!!!' ); 06 } 07 $page_title = $lang_module['example_add'];08 $add = 0; 09 $error = ""; 10 11 if ( $nv_Request->get_int( 'add', 'post' ) == 1 )12 { 13 $title = filter_text_input( 'title', 'post', '', 1 );14 if ( empty( $title ) ) 15 { 16 17 } 18 else 19 { 20 21 $error = $lang_module['err_title'];22

23 $query = "INSERT INTO `" . NV_PREFIXLANG . "_" . $module_data . "` (`id`,`title`) VALUES (NULL, " . $db->dbescape( $title ) . ")"; 24 if ( $db->sql_query_insert_id( $query ) )25 { 26 $db->sql_freeresult(); 27 Header( "Location: " . NV_BASE_ADMINURL . "index.php?" .28 NV_NAME_VARIABLE . "=" . $module_name . "" );29 die(); 30 31 } 32 else 33 { 34 35 } 36 } 37 } 38 39 $error = $lang_module['errorsave'];

40 $contents = call_user_func("add", $error); include ( NV_ROOTDIR . "/includes/header.php" ); echo nv_admin_theme( $contents ); 41 include ( NV_ROOTDIR . "/includes/footer.php" );42

Page 14: Hướng dẫn viết module cho phiên bản NukeViet 3

43 ?>

Sửa lại file modules/example/admin/edit.php

view source print? 01 <?php 02 03 if ( ! defined( 'NV_IS_EXAMPLE_ADMIN' ) )04 { 05 die( 'Stop!!!' ); 06 }

07 08 $error = ""; 09 $page_title = $lang_module['example_edit'];10 11 $id = $nv_Request->get_int( 'id', 'get,post', 0 );12 if ( $id > 0 ) 13 { 14 if ( $nv_Request->get_int( 'save', 'post' ) == 1 )15 { 16 $title = filter_text_input( 'title', 'post', '', 1 );17 if ( empty( $title ) ) 18 {

19 20 } 21 else 22 {

23 24 $error = $lang_module['err_title'];

25

26 $query = "UPDATE `" . NV_PREFIXLANG . "_" . $module_data . "` SET `title` = " . $db->dbescape( $title ) . " WHERE `id` =" . $id . "";

27 $db->sql_query( $query ); 28 $db->sql_freeresult();

29 Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "" ); 30 }

31 32 } 33 else 34 {

Page 15: Hướng dẫn viết module cho phiên bản NukeViet 3

35

36 $sql = "SELECT title FROM `" . NV_PREFIXLANG . "_" . $module_data . "` WHERE `id` = '" . $id . "'"; 37 $resuilt = $db->sql_query( $sql );38 list( $title ) = $db->sql_fetchrow( $resuilt );39 } 40 $contents = call_user_func("edit",$id, $title, $error);41 } 42 include ( NV_ROOTDIR . "/includes/header.php" );43 echo nv_admin_theme( $contents );44 include ( NV_ROOTDIR . "/includes/footer.php" );

45 46 ?>

Thêm file themes/admin_default/modules/example.php. Lưu ý: Tên file trong thư mục module của template admin phải trùng tên với tên module.

view source print? 01 <?php 02 function add($error) 03 { 04 global $global_config, $lang_module,$module_data, $db_config, $nv_Request,05 $module_name, $op; 06 $xtpl = new XTemplate ( "example.tpl", NV_ROOTDIR . "/themes/" . 07 $global_config ['module_theme'] . "/modules" );08 $xtpl->assign('LANG',$lang_module);09 $xtpl->assign('NV_BASE_ADMINURL',NV_BASE_ADMINURL);10 $xtpl->assign('NV_NAME_VARIABLE',NV_NAME_VARIABLE);11 $xtpl->assign('MODULE_NAME', $module_name);12 $xtpl->assign('OP', $op); 13 if(!empty($error)) 14 { 15 $xtpl->assign('ERROR',$error);16 $xtpl->parse('add.error'); 17 } 18 $xtpl->assign('NV_OP_VARIABLE',NV_OP_VARIABLE);19 $xtpl->parse('add'); 20 return $xtpl->text('add'); 21 } 22 function edit( $id, $title, $error)23 {

Page 16: Hướng dẫn viết module cho phiên bản NukeViet 3

24 global $global_config, $lang_module,$module_data, $db_config, $nv_Request,25 $module_name, $op; 26 $xtpl = new XTemplate ( "example.tpl", NV_ROOTDIR . "/themes/" . 27 $global_config ['module_theme'] . "/modules" );28 $xtpl->assign('LANG',$lang_module);29 $xtpl->assign('NV_BASE_ADMINURL',NV_BASE_ADMINURL);30 $xtpl->assign('NV_NAME_VARIABLE',NV_NAME_VARIABLE);31 $xtpl->assign('MODULE_NAME', $module_name);32 $xtpl->assign('TITLE', $title);33 $xtpl->assign('ID', $id); 34 $xtpl->assign('OP', $op); 35 if(!empty($error)) 36 { 37 $xtpl->assign('ERROR',$error);38 $xtpl->parse('edit.error');39 } 40 $xtpl->assign('NV_OP_VARIABLE',NV_OP_VARIABLE);41 $xtpl->parse('edit'); 42 return $xtpl->text('edit');43 } 44 function main($content) 45 { 46 global $global_config, $lang_module,$module_data, $db_config, $nv_Request,47 $module_name, $op; 48 $xtpl = new XTemplate ( "example.tpl", NV_ROOTDIR . "/themes/" . 49 $global_config ['module_theme'] . "/modules" );50 $xtpl->assign('LANG',$lang_module);51 if(!empty($content)) 52 { 53 foreach($content as $i => $row)54 { 55 $xtpl->assign('CLASS',($i % 2) ? '' : ' class="second"'); 56 $xtpl->assign('EDIT',"<a href=\"index.php?" . NV_NAME_VARIABLE . "=" .

57 $module_name . "&" . NV_OP_VARIABLE . "=edit&amp;id=" . $row['id'] . "\">" . 58 $lang_module['example_edit'] . "</a>");59 $xtpl->assign('DEL',"<a href=\"index.php?" . NV_NAME_VARIABLE . "=" .60 $module_name . "&" . NV_OP_VARIABLE . "=del&amp;id=" . $row['id'] . "\">" .61 $lang_module['example_del'] . "</a>");62 $xtpl->assign('CONTENT', $row);63 $xtpl->parse('main.loop'); 64 } 65 }

Page 17: Hướng dẫn viết module cho phiên bản NukeViet 3

66 $xtpl->parse('main'); 67 return $xtpl->text('main');68 } 69 ?>

Thêm file themes/admin_default/modules/example.tpl

view source print? 01 <!-- BEGIN: add --> 02 <form action="{NV_BASE_ADMINURL}index.php" method="POST"> 03 <table class="tab1"> 04 <tr> 05 <td width="30%"><strong>{LANG.title}</strong></td>06 <td><input name="title" type="text" size="100" /></td>07 </tr> 08 </table> 09 <table class="tab2" align="center">10 <tr> 11 <td style="text-align:center;">12 <input type="submit" value="{LANG.save} "/>13 <input name="add" type="hidden" value="1" />14 <input type="hidden" name =" {NV_NAME_VARIABLE} "value="15 {MODULE_NAME} " /> 16 </tr> 17 </td> 18 <input type="hidden" name =" {NV_OP_VARIABLE} "value=" {OP} " /> 19 </table> 20 </form> 21 <!-- BEGIN: error --> 22 <div class="quote" style="width:780px;">23 <blockquote class="error"><span>{ERROR}</span></blockquote> 24 </div> 25 <div class="clear"></div> 26 <!-- END: error --> 27 <!-- END: add --> 28 <!-- BEGIN: edit --> 29 <form action="{NV_BASE_ADMINURL}index.php" method="POST"> 30 <table class="tab1"> 31 <tr> 32 <td width="30%"><strong>{LANG.title}</strong></td>33 <td><input name="title" type="text" size="100" value="{TITLE}" /></td>34 </tr>

Page 18: Hướng dẫn viết module cho phiên bản NukeViet 3

35 </table> 36 <table class="tab2" align="center">37 <tr> 38 <td style="text-align:center;">39 <input type="submit" value="{LANG.save} "/>40 <input name="add" type="hidden" value="1" />41 <input type="hidden" name =" {NV_NAME_VARIABLE} "value="42 {MODULE_NAME} " /> 43 <input type="hidden" name ="id" value="{ID}" />44 <input type="hidden" name =" {NV_OP_VARIABLE} "value=" {OP} " /> 45 </tr> 46 </td> 47 </table> 48 </form> 49 <!-- BEGIN: error --> 50 <div class="quote" style="width:780px;">51 <blockquote class="error"><span>{ERROR}</span></blockquote> 52 </div> 53 <div class="clear"></div> 54 <!-- END: error --> 55 <!-- END: edit --> 56 <!-- BEGIN: main --> 57 <table class="tab1"> 58 <thead> 59 <tr style="text-align:center;">60 <td>{LANG.id}</td> 61 <td>{LANG.title}</td> 62 <td>{LANG.func}</td> 63 </tr> 64 <!-- BEGIN: loop --> 65 <tbody{CLASS}> 66 <tr> 67 <td style="text-align:center;">{CONTENT.id}</td>68 <td>{CONTENT.title}</td> 69 <td style="text-align:center;">70 {EDIT} - {DEL} 71 </td> 72 </td> 73 </tbody> 74 <!-- END: loop --> 75 </table> 76 <!-- END: main -->

Page 19: Hướng dẫn viết module cho phiên bản NukeViet 3

Danh sách các file:

$ modules/example/admin/index.html $ modules/example/admin/main.php $ modules/example/index.html $ modules/example/funcs/main.php $ modules/example/funcs/index.html $ modules/example/admin.functions.php $ modules/example/functions.php $ modules/example/action.php $ modules/example/language/vi_admin.php $ modules/example/language/en_admin.php $ themes/admin_default/modules/example.php $ themes/admin_default/modules/example.tpl

Sơ đồ cấu trúc template sử dụng Xtemplate

themes —-default css —-example.css images js modules —-example.php tpl —-example.tpl

Sửa lại file modules/example/funcs/main.php

view source print? 01 <?php 02 03 if ( ! defined( 'NV_IS_MOD_EXAMPLE' ) ) die( 'Stop!!!' );

04 $sql = "SELECT `id`, `title` FROM `" . NV_PREFIXLANG . "_" . $module_data . "`"; 05 $resuilt = $db->sql_query( $sql );06 while ( $row = $db->sql_fetchrow( $resuilt ) )07 { 08 $content[] = $row; 09 } 10 $contents = call_user_func( "main_theme", $content );11 include ( NV_ROOTDIR . "/includes/header.php" );12 echo nv_site_theme( $contents );

Page 20: Hướng dẫn viết module cho phiên bản NukeViet 3

13 include ( NV_ROOTDIR . "/includes/footer.php" );14 ?>

Thêm mới file modules/example/theme.php với nội dung như sau: Chú ý:Tên của file trong thư mục modules của template phải trùng với tên của module.

view source print? 01 <?php 02 03 function main_theme($content)04 { 05 global $module_file, $global_config;

06 $xtpl = new XTemplate("example.xtpl", NV_ROOTDIR . "/themes/" . $module_info['template'] . "/modules/" . $module_file );

07 $a = 1; 08 foreach($content as $content_i)09 { 10 $xtpl->assign('CONTENT',$content_i);11 $xtpl->assign('STT',$a); 12 $xtpl->parse('main.loop'); 13 $a++; 14 } 15 $xtpl->parse('main'); 16 return $xtpl->text('main');17 } 18 19 ?>

Thêm mới file themes/default/modules/example/example.xtpl

view source print? 01 <!-- BEGIN: main --> 02 <table> 03 <!-- BEGIN: loop --> 04 <tr> 05 <td>{STT}</td> 06 <td>{CONTENT.title}</td> 07 </tr> 08 <!-- END: loop --> 09 </table> 10 <!-- END: main -->

Page 21: Hướng dẫn viết module cho phiên bản NukeViet 3

Nếu bạn cần tạo ngôn ngữ cho ngoài site bạn có thể tạo thêm các file: vi.php, en.php trong thư mục language của module

Khi đó danh mục các file của module gồm:

$ modules/example/admin/index.html $ modules/example/admin/main.php $ modules/example/index.html $ modules/example/funcs/main.php $ modules/example/funcs/index.html $ modules/example/admin.functions.php $ modules/example/functions.php $ modules/example/theme.php $ modules/example/action.php $ modules/example/version.php $ modules/example/language/vi.php $ modules/example/language/vi_admin.php $ modules/example/language/en.php $ modules/example/language/en_admin.php $ themes/default/modules/example.php $ themes/default/modules/example/example.xtpl $ themes/admin_default/modules/example.php $ themes/admin_default/modules/example.tpl

Hướng dẫn viết block cho phiên bản NukeViet 3.0 August 23, 2010 BỞI xman 9 Comments

Lâu rồi Xman không viết bài hướng dẫn nào cho mọi người vì bận nhiều việc riêng quá. Hôm nay tranh thủ viết bài hướng dẫn cách bạn viết block cho phiên bản NukeViet 3.0.

Yêu cầu: -Phiên bản NukeViet 3.0 -Một trình soạn thảo text

OK, bài này mình sẽ hướng dẫn một cách chung nhất và bạn có thể viết được 1 block theo ý mình.

Đối với các blocks dạng global nằm trong thư mục includes/blocks mà bạn muốn sử dụng xuyên suốt trên toàn bộ các khu vực tức là có thể chạy trên tất cả các module cách thức đặt tên như sau:

view source print?

Page 22: Hướng dẫn viết module cho phiên bản NukeViet 3

1 global.tênblock.php

trong đó global là bắt buộc để hệ thống nhận dạng đó là block.

Đối với các blocks của module thường nằm trong thư mục modules/tênmodule/blocks thì cách thức đặt tên như sau:

view source print? 1 module.tênblock.php

trong đó bắt buộc phải bắt đầu bằng từ “module”. Do đó các bạn xác định sẽ viết block dạng nào và thuộc module nào thì nhớ là đặt đúng vị trí thư mục và đúng tên.

Cấu trúc 1 block của NV (NukeViet) bạn có thể thấy ví dụ như block global about như sau:

view source print? 01 <?php 02 03 /** 04 * @Project NUKEVIET 3.0 05 * @Author VINADES.,JSC ([email protected])06 * @Copyright (C) 2010 VINADES., JSC. All rights reserved 07 * @Createdate 3/25/2010 18:608 */ 09 if ( ! defined( 'NV_SYSTEM' ) ) die( 'Stop!!!' );

10 11 if ( ! function_exists( 'nv_message_about' ) )

12 { 13 function nv_message_about()14 { 15 global $global_config, $db, $lang_global, $site_mods, $module_file;16

17 $sql = "SELECT `id`,`title`,`alias`,`bodytext` FROM `" . NV_PREFIXLANG . "_about` WHERE status = 1 LIMIT 1";

18 $result = $db->sql_query( $sql );

19 $num = $db->sql_numrows( $result );

20 if ( $num ) 21 {

22 list( $id, $title, $alias, $bodytext ) = $db->sql_fetchrow( $result );

23 $link = NV_BASE_SITEURL . "?" . NV_LANG_VARIABLE . "=" .

Page 23: Hướng dẫn viết module cho phiên bản NukeViet 3

NV_LANG_DATA . "&amp;" . NV_NAME_VARIABLE . "=about&amp;" . NV_OP_VARIABLE . "=" . $alias;

24 $bodytext = strip_tags( $bodytext );

25 $bodytext = nv_clean60( $bodytext, 300 );

26 $xtpl = new XTemplate( "global.about.tpl", NV_ROOTDIR . "/themes/" . $global_config['site_theme'] . "/blocks/" );

27 $xtpl->assign( 'LINK', $link );

28 $xtpl->assign( 'TITLE', $title );

29 $xtpl->assign( 'BODYTEXT', $bodytext );

30 $xtpl->parse( 'main' );

31 $content = $xtpl->text( 'main' );

32 return $content;33 } 34 } 35 } 36 37 $content = nv_message_about();38 39 ?>

Phần mở đầu chính là thông tin về tác giả và mô tả về block các bạn có thể bỏ qua cũng được

view source print? 1 /** 2 * @Project NUKEVIET 3.0 3 * @Author VINADES.,JSC ([email protected])4 * @Copyright (C) 2010 VINADES., JSC. All rights reserved5 * @Createdate 3/25/2010 18:66 */

Tiếp theo đó là phần khai báo kiểm tra xem có thuộc hệ thống hay không và chống truy cập trực tiếp.

Đối với block dạng global thì ta sẽ khai báo như sau:

view source print? 1 if ( ! defined( 'NV_SYSTEM' ) ) die( 'Stop!!!' );

Đối với block của module thì ta sẽ khai báo cú pháp như sau:

view source print?

Page 24: Hướng dẫn viết module cho phiên bản NukeViet 3

1 if ( ! defined( 'NV_IS_MOD_TENMODULE' ) ) die( 'Stop!!!' );

trong đó TENMODULE chính là tên thư mục module mà bạn muốn viết block cho module này

Tiếp theo là :

view source print? 1 if ( ! function_exists( 'nv_message_about' ) )2 { 3 function nv_message_about() 4 { 5 ....code content... 6 } 7 }

Nhiệm vụ của dòng này là php sẽ kiểm tra xem hàm “nv_message_about” có tồn tại hay chưa nếu chưa thì tiến hành khai báo hàm này nếu có rồi thì sẽ không khai báo nữa, hàm này là tuỳ ý bạn đặt tên, lý do đó chính là hệ thống NV cho phép bạn thêm trên 1 trang nhiều nhiều block cùng 1 file do đó nếu trên cùng 1 trang và khai báo hàm đã được khai báo sẽ phát sinh lỗi.

Ngoài ra thông thường nếu block của bạn không cần phải xử lý những chức năng hay dữ liệu bằng việc tạo ra các hàm xử lý đặc biệt thì có thể bỏ qua không cần khai báo dòng trên.

OK, trong đoạn trên các bạn để ý dòng “…code content…” đây chính là phần bạn cần lập trình để tạo ra block theo mục đích của mình.

Để có thể thực hiện và lấy dữ liệu từ hệ thống 1 cách chính xác bạn cần phải hiểu qua một vài biến global mà hệ thống cung cấp cho bạn:

view source print? 1 global $global_config, $db, $lang_global, $module_file,....;

$global_config: đây chính là mảng chứa những giá trị cấu hình của hệ thống nếu bạn cần. $db: đây chính là 1 object cho phép bạn truy cập những method được xây dựng sẵn trong class mysql của hệ thông $lang_global: đây chính là mảng chứa giá trị là những định nghĩa ngôn ngữ xây dựng sẵn của hệ thống $module_name: đây chính là tên module đang được truy cập đến. $module_data: đây chính là tên bảng mysql được thiết lập và tạo ra trong csdl mà module đang được truy cập đến để lấy dữ liệu.

Page 25: Hướng dẫn viết module cho phiên bản NukeViet 3

Thông thường nếu bạn là 1 người phát triển 1 module nào đó những biến mà bạn tạo ra nếu có thể dùng được và cho phép dùng ở dạng global ở block thì các bạn có thể khai báo thêm vào phần phía sau trong dòng code trên.

CONSTANT VARIBALE:

NV_PREFIXLANG: đây chính là kết hợp giữa giá trị $db_config['prefix'] và NV_LANG_DATA tức là sẽ lấy giá trị tiền tố trong quá trình bạn cài đặt NV và viết tắt của ngôn ngữ hiện tại đang dùng có thể là “vi” hoặc “en” hoặc 1 ngôn ngữ nào đó mà trên hệ thống của bạn có. NV_BASE_SITEURL : biến này sẽ trả về giá trị là thư mục hoặc cấp cao nhất mà website của bạn đang chạy ví dụ đường dẫn website của bạn là http://user.vn/ thì hàm này trả về là “/” NV_LANG_VARIABLE: biến này trả về giá trị chính là “lang” NV_LANG_DATA: như đã giải thích ở NV_PREFIXLANG NV_NAME_VARIABLE: biến này trả về giá trị ví dụ là “nv” NV_OP_VARIABLE: biến này trả về giá trị ví dụ là “op” NV_ROOTDIR: biến này trả về chính là đường dẫn vật lý trên đĩa cứng đang chứa thư mục chạy nukeviet của bạn ví dụ như của xman là : D:/xampp/htdocs/nukeviet3svn lưu ý là không có dấu / ở cuối cùng

Sau đây là hướng dẫn cách bạn truy xuất vào csdl của nukeviet. Trước khi thực hiện bạn phải xác định được bạn muốn lấy dữ liệu từ bảng nào, và truy xuất bao nhiêu row trong csdl. Ví dụ sau đấy là lấy ra các bản ghi trong module about tạo ra tôi sẽ làm như sau

view source print?

1 $sql = "SELECT `id`,`title`,`alias`,`bodytext` FROM `" . NV_PREFIXLANG . "_about` WHERE status = 1 LIMIT 1"; 2 $result = $db->sql_query( $sql );

Thông thường thì khi lấy dữ liệu thì sẽ kèm theo nó là lấy dữ liệu theo điều kiện ví dụ ở đây là WHERE status = 1 tức là tôi chỉ lấy SELECT những bản ghi có trường “status” = 1 tương đương với việc bản ghi này ý nghĩa có thể cho phép hoạt động trên site trong bảng FROM `” . NV_PREFIXLANG . “_about` . Trường “status” chinh là trường mà người viết module đặt ra và sẽ khác nhau với mỗi module ví dụ có module sẽ là “active” hoặc khác do đó không cố định. Nếu bạn không lấy theo điều kiện nào thì có thể bỏ câu lệnh WHERE đằng sau này.

Trong câu lệnh trên tôi chỉ lấy ra id, title, alias và bodytext từ bảng about và với điều kiện status là 1 Để trả về kết quả có thực hiện câu lệnh sql trên được hay không tôi sử dụng gán kết quả vào biến $result bằng phương thức $db->sql_query($sql):

view source print? 1 $result = $db->sql_query( $sql );

Page 26: Hướng dẫn viết module cho phiên bản NukeViet 3

Ở đây mỗi lần truy cập tôi chỉ lấy được 1 bản ghi do đó để lấy hết số lượng bản ghi đáp ứng được điều kiện của tôi thì tôi sử dụng vòng lặp WHILE:

view source print?

1 while ( list( $id, $listcatid, $publtime, $exptime, $title, $alias, $homeimgthumb ) = $db->sql_fetchrow( $result ) )

Trong câu lệnh trên tôi dùng hàm list và gán các giá trị lấy được từ csdl vào biến tương ứng với số field trong câu lệnh SELECT phía trên. Để lấy được các bản ghi tôi sử dụng phương thức $db->sql_fetchrow:

view source print? 1 $db->sql_fetchrow( $result )

và $result chính là kết quả trả về từ $db->sql_query

Sau khi đã có dữ liệu chính là các biến:

view source print? 1 $id, $listcatid, $publtime, $exptime, $title, $alias, $homeimgthumb

thì các bạn có thể thay đổi hoặc sửa đổi tuỳ theo ý thích của mình và điều quan trọng sau cùng đó là muốn hiển thị kết quả ra cho người dùng như thế nào.

Để xuất dữ liệu hiển thị ra ngoài bạn chỉ việc đẩy các giá trị vào biến:

view source print? 1 $content

Ví dụ tôi chỉ xuất đơn thuần các biến ra ngoài mà chưa định dạng html hiển thị như sau:

view source print?

1 $content = $id. $listcatid. $publtime. $exptime. $title. $alias. $homeimgthum;

Ok trên đây là cách thức xây dựng 1 block đơn thuần và vấn đề của bạn là ứng dụng nó vào kết hợp với các mã html cho các biến trên để dịnh dạng đẹp mắt hơn cho việc hiển thị ra bên ngoài. Cuối cùng là vào admin và thử thêm block này và chiêm ngưỡng thành quả.

Chúc các bạn thành công, Xman !

Page 27: Hướng dẫn viết module cho phiên bản NukeViet 3

-- Cấu trúc một module, chức năng các file, các file, floder bắt buộc phải có. -- Cách viết một module đơn giản chỉ chứa file PHP. -- Mở rộng kết hợp file PHP riêng ngoài các file bắt buộc phải có. -- Làm việc với file ngôn ngữ lang. -- Sử dụng xtemplate. -- Mở rộng thêm các file thông tin module, RSS -- Sử dụng javscript và ajax cho module (phần AJAX các bạn xem tại viewtopic.php?f=117&t=15403). -- Sử dụng lớp (class) đã được xây dựng sẵn. -- Tích hợp thêm các phần mở rộng Trước khi vào phần hướng dẫn mình xin trình bày một số quy ước. Các file PHP được bắt đầu với phần thông tin về tác giả, bản quyền, ngày viết, email tác giả.. VÍ dụ như: Mã: Chọn tất cả

/** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @copyright 2011 * @createdate 26/01/2011 09:17 AM */

Tương tự cho các file js

Mã: Chọn tất cả /* * * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @copyright 2011 * @createdate 26/01/2011 09:17 AM */

Tên module tốt nhất các bạn chỉ đặt tên bằng chữ cái không chứa các kí tự đặc biệt. Bây giờ ta vào phần chi tiết. ---- CẤU TRÚC CƠ BẢN CỦA MỘT MODULE Bao gồm các file và thư mục sau: - admin.functions.php - version.php - funcs - funcs/main.php - admin - admin/main.php - functions.php Với nhiêu đó là ta đã có một module đơn giản (không có CSDL) có thể hoạt động rồi. Như vậy

Page 28: Hướng dẫn viết module cho phiên bản NukeViet 3

muốn viết một module đầu tiên ta tạo một thư mục có tên là tên của module trong thư mục modules. Sau đó lần lượt tạo các thư mục, file như trên vào thư mục vừa tạo. Mình sẽ ví dụ đây là module quanlihs. Chức năng của các file như sau: -- file version.php: file này có chức năng khai báo tiêu đề module, các funcs có block, tác giả module, thông tin phiên bản, cấu trúc thư mục trong thư mục uploads. -- File admin.functions.php: File này thường chứa các function, hằng dùng trong admin -- File function.php: File này thường chứa các function, hằng dùng cho ngoài site -- admin/main.php: File này sẽ thể hiện nội dung của module phần admin -- funcs/main.php: FIle này sẽ thể hiện nội dung (trang chính) của module bên ngoài site. Thứ tự khởi động một module như sau: Khi module được chạy thì tùy theo admin hay ngoài site mà file admin.functions.php hay file function.php được chạy trước sau đó là các file trong thư mục admin hay funcs được chạy tiếp theo mặc định sẽ là file main.php. Ta thường thấy url trang web nukeviet (chưa bật rewrite) có dạng http://yourdomain/index.php?lang=vi&nv= ... =listenone Trong đó lang chính là ngôn ngữ của site, nv là module đang chạy, op chính là funcs đang chạy (ở đây là listenone). Giá trị op này chính là tên của funsc trong thư mục funsc hay admin. Nếu trên url mà khuyết phần op= có nghĩa funcs main.php đang được chạy. Cấu trúc url như trên là cấu trúc cơ bản. Giá trị op ta có thể thay đổi ví dụ như http://nukeviet.vn/vi/news/viec-lam/ . Ta sẽ tìm hiểu sau. Bây giờ chúng ta bắt đầu viết một module đơn giản (không có CSDL) để xuất ra dòng chữ "XIN CHÀO CÁC BẠN" bên trong admin và ngoài site. Đầu tiên tạo một thư mục (là tên module) trong thư mục modules rồi thêm vào đó các file và thư mục sau: - admin.functions.php - version.php - funcs - funcs/main.php - admin - admin/main.php - functions.php file version.php có nội dung: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @copyright 2011 * @createdate 05/12/2010 09:47 */

Page 29: Hướng dẫn viết module cho phiên bản NukeViet 3

if ( ! defined( 'NV_ADMIN' ) or ! defined( 'NV_MAINFILE' )) die( 'Stop!!!' ); $module_version = array( "name" => "QuanLyHocSinh", // Tieu de module "modfuncs" => "main" , "is_sysmod" => 0, "virtual" => 1, "version" => "3.0.01", "date" => "Wed, 26 Jan 2011 12:47:15 GMT", "author" => "PHAN TAN DUNG (email: [email protected])", "note"=>"", "uploads_dir" => array( $module_name ) ); ?>

Dòng này: if ( ! defined( 'NV_ADMIN' ) or ! defined( 'NV_MAINFILE' )) die( 'Stop!!!' ); các bạn để nguyên ở mọi module -"modfuncs" => "main": Tên các funcs trong thư mục funcs có block khi hoạt động bên ngoài site. Đối với các funcs không có block ví dụ như để xử lý AJAX, gửi email (popup) thì không cần khai báo. Các funcs được phân cách nhau bởi dấu "," ví dụ: Mã: Chọn tất cả

"modfuncs" => "main, funcs1, funcs2, funcs3" , -"is_sysmod" => 0, : Có phảo module hệ thông hay không 0: không 1: có. -"virtual" => 1: CHo phép ảo hóa module không 0: không 1: có -date,author, version: thông tin ngày , tác giả, phiên bản module. -uploads_dir: Khai báo cấu trúc thư mục trong thư mục uploads. Với khai báo như trên thì khi kích hoạt module hệ thông sẽ tạo một thư mục có tên là tên của module trong thư mục upload (biến $module_name được hiểu là tên module) file admin.functions.php có nội dung như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @Copyright 2011 * @createdate 26/01/2011 10:08 AM */ if ( ! defined( 'NV_ADMIN' ) or ! defined( 'NV_MAINFILE' ) or ! defined( 'NV_IS_MODADMIN' ) ) die( 'Stop!!!' ); $allow_func = array('main'); define( 'NV_IS_MUSIC_ADMIN', true ); ?>

- $allow_func = array('main'); : biến $allow_func là một mảng một chiều chứa các funcs rong

Page 30: Hướng dẫn viết module cho phiên bản NukeViet 3

thư mục admin cho phép chạy. Ở ví dụ này chỉ cho một funcs là main.php. Các funcs không được khai báo trong biến này khi chạy sẽ báo lỗi "Bạn không có quyền truy cập chức năng này." - define( 'NV_IS_QUANLY_ADMIN', true ); Dòng này sẽ khởi tạo một hằng dùng cho module. Các funcs như main.php sẽ kiểm tra giá trị hằng này nếu đúng sẽ chạy ngược lại sẽ báo lỗi "Stop!!!". file function.php các bạn thêm như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @copyright 2011 * @createdate 26/01/2011 10:10 AM */ if (!defined('NV_SYSTEM')) die('Stop!!!'); define('NV_IS_MOD_QUANLY', true); ?>

Với module đơn giản như ví dụ này thì file này chỉ có chức năng tạo một hằng để dùng bên ngoài site thôi. Tạo file main.php trong thư mục admin với nội dung như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Phan Tan Dung ([email protected]) * @Copyright (C) 2011 * @Createdate 26-01-2011 14:43 */ if ( ! defined( 'NV_IS_QUANLY_ADMIN' ) ) die( 'Stop!!!' ); $page_title = "Trang chính"; $contents = "Xin chào các bạn"; include (NV_ROOTDIR . "/includes/header.php"); echo nv_admin_theme($contents); include (NV_ROOTDIR . "/includes/footer.php"); ?>

- Biến $page_title là tiêu đề của funcs hiện tại. - Tất cả nội dung sẽ được lưu vào một biến $contents sau đó sẽ được xuất ra sau khi gọi file header.php. Phần : Mã: Chọn tất cả

include (NV_ROOTDIR . "/includes/header.php"); echo nv_admin_theme($contents); include (NV_ROOTDIR . "/includes/footer.php");

Các bạn giữ nguyên mọi module (trong admin) Tương tự tạo file main.php lưu vào thư mục funcs với nội dung như sau: Mã: Chọn tất cả

Page 31: Hướng dẫn viết module cho phiên bản NukeViet 3

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung * @Copyright (C) 2011 * @Createdate 26/01/2011 10:26 AM */ if ( ! defined( 'NV_IS_MOD_QUANLY' ) ) die( 'Stop!!!' ); $page_title = $module_info['custom_title']; $key_words = $module_info['keywords']; $contents = "Xin chào các bạn"; include ( NV_ROOTDIR . "/includes/header.php" ); echo nv_site_theme( $contents ); include ( NV_ROOTDIR . "/includes/footer.php" ); ?>

OK như vậy ta đã tạo xong một module đơn giản để xuất ra dong chữ "Xin chào các bạn" bên trong admin lẫn ngoài site. Bây giờ các bạn vào phần thiết lập module mới kích hoạt modlue vừa tạo rồi vào phần quản lí của module và noài site bạn sẽ thấy kết quả Với module Đơn giản này ta có thể sử dụng để inframe trang khác hoặc liên kết sang trang khác khi ấn vào menu. Ví dụ thay file main.php bằng: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung * @Copyright (C) 2011 * @Createdate 26/01/2011 10:26 AM */ if ( ! defined( 'NV_IS_MOD_QUANLY' ) ) die( 'Stop!!!' ); $page_title = $module_info['custom_title']; $key_words = $module_info['keywords']; $contents = '<iframe id="I1" name="I1" src="http://24h.com.vn/" style="width: 1029px; height: 693px"> Your browser does not support inline frames or is currently configured not to display inline frames. </iframe> '; include ( NV_ROOTDIR . "/includes/header.php" ); echo nv_site_theme( $contents ); include ( NV_ROOTDIR . "/includes/footer.php" ); ?>

Ta có module inframe của trang 24h Hay thay file main.php bằng : Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung

Page 32: Hướng dẫn viết module cho phiên bản NukeViet 3

* @Copyright (C) 2011 * @Createdate 26/01/2011 10:26 AM */ if ( ! defined( 'NV_IS_MOD_QUANLY' ) ) die( 'Stop!!!' ); $page_title = $module_info['custom_title']; $key_words = $module_info['keywords']; $contents = ''; Header( "Location: http://nhaccuateen.info" ) ; include ( NV_ROOTDIR . "/includes/header.php" ); echo nv_site_theme( $contents ); include ( NV_ROOTDIR . "/includes/footer.php" ); ?>

Ta có module khi vào thì sẽ chuyển sang trang http://nhaccuateen.info. Hay các bạn có thể dùng vào nhiều việc khác Tiếp theo chúng ta sẽ thêm file action.php làm nhiệm vụ thao tác với CSDL khi cài đặt cũng như xóa module. Bây giờ ta thêm một table trong CSDL để quản lí học sinh trong lớp (STT, họ tên, ngày sinh, địa chỉ). Tạo file action.php đặt cùng thư mục với file functions.php với nội dung như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @copyright 2011 * @createdate 26/01/2011 10:10 AM */ if( !defined('NV_IS_FILE_MODULES') ) die('Stop!!!'); $sql_drop_module = array(); $sql_drop_module[] = "DROP TABLE IF EXISTS `" . $db_config['prefix'] . "_" . $lang . "_" . $module_data . "`"; $sql_create_module = $sql_drop_module; $sql_create_module[] = "CREATE TABLE `" . $db_config['prefix'] . "_" . $lang . "_" . $module_data . "` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `stt` INT( 255 ) unsigned NOT NULL, `hoten` varchar(255) NOT NULL, `ngaysinh` INT( 11 ) NOT NULL DEFAULT '0', `diachi` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM"; ?>

Các bạn vào phần quản lí module cài lại module sẽ thấy trong CSDL có thêm một table. Mình

Page 33: Hướng dẫn viết module cho phiên bản NukeViet 3

giải thích nội dung file trên: $sql_drop_module[], $sql_create_module[] để chỉ việc xóa, tạo mới module. Biến $db_config['prefix'] là tiếp đầu tố của table, $lang là ngôn ngữ của site, $module_data thông thường được hiểu như tên của module. Nội dung bên trong dấu ngoặc kép là lệnh thao tác với CSDL không nhất thiết phải là xóa, thêm mà có thể chỉnh sửa, xóa trường, chèn dữ liệu... Biến $sql_create_module[] dạng ARRAY nên có thể thêm nhiều lệnh thao thác csdl (tạo nhiều bảng). Ta đã tạo CSDL thành công bây giờ cần phải viết code để làm việc với nó. Bây giờ phần quản lí module ta cần có thêm một submenu để thêm học sinh. Mở file admin.functions.php lên sửa lại như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @Copyright 2011 * @createdate 26/01/2011 10:08 AM */ if ( ! defined( 'NV_ADMIN' ) or ! defined( 'NV_MAINFILE' ) or ! defined( 'NV_IS_MODADMIN' ) ) die( 'Stop!!!' ); $submenu['add'] = "Thêm học sinh"; $allow_func = array('main', 'add'); define( 'NV_IS_QUANLY_ADMIN', true ); ?>

Ta thấy bây giờ có thêm $submenu['add'] có nghĩa là ta đã khai báo một submenu trong admin, biến $allow_func thêm mới 'add' có nghĩa ta đã chấp nhận thêm một funcs mới có tên add (add.php). Bây giờ các bạn vào phần quản lí module sẽ thấy có thêm submenu "Thêm học sinh" nhưng khi ấn vào sẽ hiện : "Lỗi truy cập 404 Lỗi 404: Trang web mà bạn đã cố gắng truy cập không tồn tại trên máy chủ của website." có nghĩa là chưa có file add.php. Ta thêm file add.php vào thư mục admin với nội dung như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Phan Tan Dung ([email protected]) * @Copyright (C) 2011 * @Createdate 26-01-2011 14:43 */ if ( ! defined( 'NV_IS_QUANLY_ADMIN' ) ) die( 'Stop!!!' ); $page_title = "Thêm học sinh"; $my_head = "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/popcalendar/popcalendar.js\"></script>\n"; $my_head .= "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL

Page 34: Hướng dẫn viết module cho phiên bản NukeViet 3

. "js/shadowbox/shadowbox.js\"></script>\n"; $my_head .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.css\" />\n"; $my_head .= "<script type=\"text/javascript\">\n"; $my_head .= "Shadowbox.init({\n"; $my_head .= "});\n"; $my_head .= "</script>\n"; $contents = ""; $error = ""; $data = array(); $data['hoten'] = filter_text_input( 'hoten', 'post', '' ); $data['ngaysinh'] = filter_text_input( 'ngaysinh', 'post', '', 1, 10 ); unset( $m ); if ( preg_match( "/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})$/", $data['ngaysinh'], $m ) ) { $data['ngaysinh1'] = mktime( 0, 0, 0, $m[2], $m[1], $m[3] ); } else { $data['ngaysinh1'] = ""; } $data['diachi'] = $nv_Request->get_string( 'diachi', 'post', '' ); if ( ($nv_Request->get_int( 'add', 'post', 0 ) == 1) ) { if ( $data['hoten'] == "" ) { $error = "Bạn chưa nhập tên học sinh"; } elseif ( $data['ngaysinh'] == "" ) { $error = "Bạn chưa nhập ngày sinh"; } elseif ( $data['diachi'] == "" ) { $error = "Bạn chưa nhập địa chỉ"; } else { $sql = "SELECT `stt` FROM `" . NV_PREFIXLANG . "_" . $module_data . "` ORDER BY stt DESC LIMIT 0,1"; $resuilt = $db->sql_query( $sql ); list ( $currentstt ) = $db->sql_fetchrow( $resuilt ); $newstt = $currentstt + 1; $query = "INSERT INTO `" . NV_PREFIXLANG . "_" . $module_data . "` ( `id`, `stt`, `hoten`, `ngaysinh`, `diachi` ) VALUES ( NULL,

Page 35: Hướng dẫn viết module cho phiên bản NukeViet 3

" . $newstt . ", " . $db->dbescape( $data['hoten'] ) . ", " . $data['ngaysinh1'] . ", " . $db->dbescape( $data['diachi'] ) . " )"; if ( $db->sql_query_insert_id( $query ) ) { $db->sql_freeresult(); Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name); die(); } else { $error = "Không thể lưu dữ liệu được"; } } } if( $error ) { $contents .= "<div class=\"quote\" style=\"width: 780px;\">\n <blockquote class=\"error\"> <span>".$error."</span> </blockquote> </div>\n <div class=\"clear\"> </div>"; } $contents .=" <form method=\"post\"> <table class=\"tab1\"> <thead> <tr> <td colspan=\"2\"> Thông tin học sinh mới </td> </tr> </thead> <tbody> <tr> <td style=\"width: 150px;\"> Tên học sinh </td> <td style=\"background: #eee;\"> <input name=\"hoten\" style=\"width: 470px;\" value=\"" . $data['hoten'] . "\" type=\"text\"> </td> </tr> </tbody> <tbody> <tr> <td> Ngày sinh </td> <td> <input id=\"ngaysinh\" name=\"ngaysinh\" style=\"width:

Page 36: Hướng dẫn viết module cho phiên bản NukeViet 3

470px;\" value=\"" . $data['ngaysinh'] . "\" type=\"text\" /> <img src=\"" . NV_BASE_SITEURL . "images/calendar.jpg\" style=\"cursor: pointer; vertical-align: middle;\" onclick=\"popCalendar.show(this, 'ngaysinh', 'dd.mm.yyyy', true);\" alt=\"\" height=\"17\" /> </td> </tr> </tbody> <tbody> <tr> <td> Địa chỉ </td> <td> <input name=\"diachi\" style=\"width: 470px;\" value=\"" . $data['diachi'] . "\" type=\"text\" /> </td> </tr> </tbody> <tr> <td colspan=\"2\" align=\"center\" style=\"background: #eee;\">\n <input name=\"confirm\" value=\"Lưu\" type=\"submit\">\n <input type=\"hidden\" name=\"add\" value=\"1\">\n </td>\n </tr>\n </table>\n </form>\n"; include (NV_ROOTDIR . "/includes/header.php"); echo nv_admin_theme($contents); include (NV_ROOTDIR . "/includes/footer.php"); ?>

Với đoạn code trên ta có một funcs với chức năng thêm học sinh. Mình giải thích như sau: Đoạn này là phần sử dụng biến $my_head để gọi java và CSS. Tạm thời phần này cho qua đến phần sử dụng java chúng ta sẽ quay lại. Ở đây ta đã gọi ra shadowbox.css, shadowbox.js, popcalendar.js để làm nhiệm vụ hiển thị lịch. Mã: Chọn tất cả

$my_head = "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/popcalendar/popcalendar.js\"></script>\n"; $my_head .= "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.js\"></script>\n"; $my_head .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.css\" />\n"; $my_head .= "<script type=\"text/javascript\">\n"; $my_head .= "Shadowbox.init({\n"; $my_head .= "});\n"; $my_head .= "</script>\n";

Mã: Chọn tất cả

Page 37: Hướng dẫn viết module cho phiên bản NukeViet 3

$data = array(); $data['hoten'] = filter_text_input( 'hoten', 'post', '' ); $data['ngaysinh'] = filter_text_input( 'ngaysinh', 'post', '', 1, 10 ); unset( $m ); if ( preg_match( "/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})$/", $data['ngaysinh'], $m ) ) { $data['ngaysinh1'] = mktime( 0, 0, 0, $m[2], $m[1], $m[3] ); } else { $data['ngaysinh1'] = ""; } $data['diachi'] = $nv_Request->get_string( 'diachi', 'post', '' );

Đoạn này dùng để lấy dữ liệu khi submit form và được lưu dưới dạng mảng (array). Các cách lấy dữ liệu các bạn có thể tham khảo tại bài hướng dẫn viết module đội code viết.

Mã: Chọn tất cả if ( ($nv_Request->get_int( 'add', 'post', 0 ) == 1) ) { if ( $data['hoten'] == "" ) { $error = "Bạn chưa nhập tên học sinh"; } elseif ( $data['ngaysinh'] == "" ) { $error = "Bạn chưa nhập ngày sinh"; } elseif ( $data['diachi'] == "" ) { $error = "Bạn chưa nhập địa chỉ"; }

Đoạn này sẽ kiểm tra xem dữ liệu đã được nhập chưa nếu chưa thì gán thông báo lỗi cho biến $error.

Mã: Chọn tất cả $sql = "SELECT `stt` FROM `" . NV_PREFIXLANG . "_" . $module_data . "` ORDER BY stt DESC LIMIT 0,1"; $resuilt = $db->sql_query( $sql ); list ( $currentstt ) = $db->sql_fetchrow( $resuilt ); $newstt = $currentstt + 1; $query = "INSERT INTO `" . NV_PREFIXLANG . "_" . $module_data . "` ( `id`, `stt`, `hoten`, `ngaysinh`, `diachi` ) VALUES ( NULL, " . $newstt . ",

Page 38: Hướng dẫn viết module cho phiên bản NukeViet 3

" . $db->dbescape( $data['hoten'] ) . ", " . $data['ngaysinh1'] . ", " . $db->dbescape( $data['diachi'] ) . " )"; if ( $db->sql_query_insert_id( $query ) ) { $db->sql_freeresult(); Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name); die(); } else { $error = "Không thể lưu dữ liệu được"; }

Đoạn này thực hiện việc lấy giá trị stt lớn nhất, tăng giá trị này lên một và ghi dữ liệu vào CSDL.

Mã: Chọn tất cả if( $error ) { $contents .= "<div class=\"quote\" style=\"width: 780px;\">\n <blockquote class=\"error\"> <span>".$error."</span> </blockquote> </div>\n <div class=\"clear\"> </div>"; }

Đoạn này là xuất ra lỗi (nếu có) CÒn phần cuối cùng là phần nội dung hiển thị (form). Ta vừa thực hiện xong việc thêm học sinh. Bây giờ ta cần hiển thị danh sách các học sinh. Mở file main.php trong thư mục admin và chỉnh lại như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Phan Tan Dung ([email protected]) * @Copyright (C) 2011 * @Createdate 26-01-2011 14:43 */ if ( ! defined( 'NV_IS_QUANLY_ADMIN' ) ) die( 'Stop!!!' ); $page_title = "Trang chính"; $contents = " <table class=\"tab1\"> <thead> <tr>

Page 39: Hướng dẫn viết module cho phiên bản NukeViet 3

<td> STT </td> <td> Họ và tên </td> <td> Ngày sinh </td> <td> Địa chỉ </td> </tr> </thead> <tbody>"; $sql = "SELECT * FROM `" . NV_PREFIXLANG . "_" . $module_data . "` ORDER BY stt ASC"; $resuilt = $db->sql_query( $sql ); while ( $row = $db->sql_fetchrow( $resuilt ) ) { $contents .= " <tr> <td> " . $row['stt'] . " </td> <td> " . $row['hoten'] . " </td> <td> " . date( "d/m/Y", $row['ngaysinh'] ) . " </td> <td> " . $row['diachi'] . " </td> </tr>"; } $contents .= "</tbody></table>"; include (NV_ROOTDIR . "/includes/header.php"); echo nv_admin_theme($contents); include (NV_ROOTDIR . "/includes/footer.php"); ?>

Đoạn code trên thực hiện việc đọc thông tin từ CSDL và xuất ra ngoài site qua vòng lặp while. Riêng phần ngày sinh thì dùng hàm date để định dạng theo ngày/tháng/năm. Cơ bản xong phần admin. Bây giờ ta đi tiếp bên ngoài site để thể hiện danh sách các học sinh. Chỉnh lại file main.php trong thư mục funcs như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung * @Copyright (C) 2011 * @Createdate 26/01/2011 10:26 AM

Page 40: Hướng dẫn viết module cho phiên bản NukeViet 3

*/ if ( ! defined( 'NV_IS_MOD_QUANLY' ) ) die( 'Stop!!!' ); $page_title = $module_info['custom_title']; $key_words = $module_info['keywords']; $contents = ''; $contents = " <table class=\"tab1\"> <thead> <tr> <td> STT </td> <td> Họ và tên </td> <td> Ngày sinh </td> <td> Địa chỉ </td> </tr> </thead> <tbody>"; $sql = "SELECT * FROM `" . NV_PREFIXLANG . "_" . $module_data . "` ORDER BY stt ASC"; $resuilt = $db->sql_query( $sql ); while ( $row = $db->sql_fetchrow( $resuilt ) ) { $contents .= " <tr> <td> " . $row['stt'] . " </td> <td> " . $row['hoten'] . " </td> <td> " . date( "d/m/Y", $row['ngaysinh'] ) . " </td> <td> " . $row['diachi'] . " </td> </tr>"; } $contents .= "</tbody></table>"; include ( NV_ROOTDIR . "/includes/header.php" ); echo nv_site_theme( $contents ); include ( NV_ROOTDIR . "/includes/footer.php" ); ?>

Đoạn code trên cũng tương tự như file main.php trong admin, cũng đọc dữ liệu và xuất ra.

Page 41: Hướng dẫn viết module cho phiên bản NukeViet 3

Ta vừa hoàn thành xong một module đơn giản để thực hiện việc quản lí học sinh trong lớp qua hai file main.php ta lại thấy trùng nhau ở doạn này: Mã: Chọn tất cả

$sql = "SELECT * FROM `" . NV_PREFIXLANG . "_" . $module_data . "` ORDER BY stt ASC"; $resuilt = $db->sql_query( $sql ); while ( $row = $db->sql_fetchrow( $resuilt ) )

Để tối ưu hóa hơn, bây giờ ta sẽ gộp hai đoạn đó thành một. Như vậy tiết kiệm được một chút tài nguyên (với những module lớn thì tiết kiệm đáng kể). Giải pháp để thực hiện là viết class riêng hoặc dùng function riêng. Mình sẽ giới thiệu cách dùng function đặt trong một file mở rộng ngoài những file bắt buộc. Ta thêm vào một file global.functions.php đặt ngang hàng với file action.php với nội dung như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @copyright 2011 * @createdate 26/01/2011 09:17 AM */ if ( ! defined( 'NV_MAINFILE' ) ) die( 'Stop!!!' ); function getALLstudent( ) { global $module_data, $db; $data = array() ; $result = $db->sql_query( "SELECT `stt`, `hoten`, `ngaysinh`, `diachi` FROM " . NV_PREFIXLANG . "_" . $module_data . " ORDER BY stt ASC" ); while ( list ( $stt, $hoten, $ngaysinh, $diachi ) = $db->sql_fetchrow($result) ) { $data[] = array ( "stt" => $stt, "hoten" => $hoten, "ngaysinh" => date ( "d/m/Y", $ngaysinh ), "diachi" => $diachi ); } return $data ; } ?>

Đoạn code trên là một function getALLstudent( ). global $module_data, $db là để gọi vào các biến bên ngoài. Ví dụ này gọi vào biến $module_data có giá trị như tên của module, biến $db là class thao tác với CSDL. Tất cả thông tin học sinh sẽ được lưu dưới dạng mảng hai chiều và được trả về ở lệnh return.

Page 42: Hướng dẫn viết module cho phiên bản NukeViet 3

Để sử dụng file trên thì ta mở file functions.php và admin.functions.php lên thêm vào dòng cuối cùng (phía trên ?>): Mã: Chọn tất cả

require_once NV_ROOTDIR . "/modules/" . $module_name . '/global.functions.php';

Như vậy kể từ bây giờ file global.functions.php trực tiếp tham gia vào admin lẫn bên ngoài site, thiếu file này hoặc viết sai đều xuất hiện trang trăng.

Tiếp tục sửa lại hai file main.php cho phù hợp như sau: Trong admin: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Phan Tan Dung ([email protected]) * @Copyright (C) 2011 * @Createdate 26-01-2011 14:43 */ if ( ! defined( 'NV_IS_QUANLY_ADMIN' ) ) die( 'Stop!!!' ); $page_title = "Trang chính"; $contents = " <table class=\"tab1\"> <thead> <tr> <td> STT </td> <td> Họ và tên </td> <td> Ngày sinh </td> <td> Địa chỉ </td> </tr> </thead> <tbody>"; $allSTD = getALLstudent( ); foreach ( $allSTD as $student ) { $contents .= " <tr> <td> " . $student['stt'] . " </td> <td>

Page 43: Hướng dẫn viết module cho phiên bản NukeViet 3

" . $student['hoten'] . " </td> <td> " . $student['ngaysinh'] . " </td> <td> " . $student['diachi'] . " </td> </tr>"; } $contents .= "</tbody></table>"; include (NV_ROOTDIR . "/includes/header.php"); echo nv_admin_theme($contents); include (NV_ROOTDIR . "/includes/footer.php"); ?>

Và bên ngoài site: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung * @Copyright (C) 2011 * @Createdate 26/01/2011 10:26 AM */ if ( ! defined( 'NV_IS_MOD_QUANLY' ) ) die( 'Stop!!!' ); $page_title = $module_info['custom_title']; $key_words = $module_info['keywords']; $contents = ''; $contents = " <table class=\"tab1\"> <thead> <tr> <td> STT </td> <td> Họ và tên </td> <td> Ngày sinh </td> <td> Địa chỉ </td> </tr> </thead> <tbody>"; $allSTD = getALLstudent( ); foreach ( $allSTD as $student ) { $contents .= " <tr>

Page 44: Hướng dẫn viết module cho phiên bản NukeViet 3

<td> " . $student['stt'] . " </td> <td> " . $student['hoten'] . " </td> <td> " . $student['ngaysinh'] . " </td> <td> " . $student['diachi'] . " </td> </tr>"; } $contents .= "</tbody></table>"; include ( NV_ROOTDIR . "/includes/header.php" ); echo nv_site_theme( $contents ); include ( NV_ROOTDIR . "/includes/footer.php" ); ?>

Ta vừa hoàn thành xong phần "Sử dụng file php riêng" Ngoài cấu trúc cở bản (các file, floder) bắt buộc các bạn có thể mở rộng tùy ý, có thể thêm bất kì floder, file nào tùy ý. Tiếp theo ta sẽ làm việc với file lang (ngôn ngữ) để có thể mở rộng ra nhiều ngôn ngữ khác ngoài tiếng Việt. Tạo thư mục language ngang hàng với thư mục admin. Tạo file admin_vi.php và file vi.php nằm trong thư mục vừa tạo. Đây là fie ngôn ngữ tiếng việt, dùng trong admin thì bắt đầu bằng admin_ còn ngoài site thì tên file là tên của kí tự ngôn ngữ. Các kí tự ngôn ngữ các bạn tham khảo thêm ở module news. Nội dung file admin_vi.php như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @Language Vietnamese * @Createdate Jan 26, 2011 11:30:02 AM */ if ( ! defined( 'NV_MAINFILE' ) ) { die( 'Stop!!!' ); } $lang_translator['author'] = "Phan Tan Dung ([email protected])"; $lang_translator['createdate'] = "04/03/2010, 15:22"; $lang_translator['copyright'] = "@Copyright (C) 2011"; $lang_translator['info'] = ""; $lang_translator['langtype'] = "lang_module"; $lang_module['main_page'] = "Trang chính"; $lang_module['stt'] = "STT"; $lang_module['name'] = "Họ và tên"; $lang_module['birthdate'] = "Ngày sinh";

Page 45: Hướng dẫn viết module cho phiên bản NukeViet 3

$lang_module['address'] = "Địa chỉ"; $lang_module['add_student'] = "Thêm học sinh"; $lang_module['add_student_err_name'] = "Bạn chưa nhập tên học sinh"; $lang_module['add_student_err_bd'] = "Bạn chưa nhập ngày sinh của học sinh"; $lang_module['add_student_err_ar'] = "Bạn chưa nhập địa chỉ"; $lang_module['add_student_err_save'] = "Không thể lưu dữ liệu được"; $lang_module['add_student_info'] = "Thông tin học sinh mới"; $lang_module['save'] = "Lưu"; ?>

Ta thấy có hia phần $lang_translator và $lang_module trong đó $lang_module là phần ngôn ngữ dùng cho module còn $lang_translator là thong tin tác giả, bản quyền, ngày dịch, ngôn ngữ dịch..... Để sử dụng file ngôn ngữ thì sau khi tạo đến bước trên ta cần mở tất cả các file trong thư mục admin lên, thay tất cả các dòng chữ bằng $lang_module tương ứng. Sau khi thay ta được hai file main.php và add.php như sau: main.php: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Phan Tan Dung ([email protected]) * @Copyright (C) 2011 * @Createdate 26-01-2011 14:43 */ if ( ! defined( 'NV_IS_QUANLY_ADMIN' ) ) die( 'Stop!!!' ); $page_title = $lang_module['main_page']; $contents = " <table class=\"tab1\"> <thead> <tr> <td> " . $lang_module['stt'] . " </td> <td> " . $lang_module['name'] . " </td> <td> " . $lang_module['birthdate'] . " </td> <td> " . $lang_module['address'] . " </td> </tr> </thead> <tbody>"; $allSTD = getALLstudent( ); foreach ( $allSTD as $student ) {

Page 46: Hướng dẫn viết module cho phiên bản NukeViet 3

$contents .= " <tr> <td> " . $student['stt'] . " </td> <td> " . $student['hoten'] . " </td> <td> " . $student['ngaysinh'] . " </td> <td> " . $student['diachi'] . " </td> </tr>"; } $contents .= "</tbody></table>"; include (NV_ROOTDIR . "/includes/header.php"); echo nv_admin_theme($contents); include (NV_ROOTDIR . "/includes/footer.php"); ?>

Add.php: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Phan Tan Dung ([email protected]) * @Copyright (C) 2011 * @Createdate 26-01-2011 14:43 */ if ( ! defined( 'NV_IS_QUANLY_ADMIN' ) ) die( 'Stop!!!' ); $page_title = $lang_module['add_student']; $my_head = "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/popcalendar/popcalendar.js\"></script>\n"; $my_head .= "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.js\"></script>\n"; $my_head .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.css\" />\n"; $my_head .= "<script type=\"text/javascript\">\n"; $my_head .= "Shadowbox.init({\n"; $my_head .= "});\n"; $my_head .= "</script>\n"; $contents = ""; $error = ""; $data = array(); $data['hoten'] = filter_text_input( 'hoten', 'post', '' ); $data['ngaysinh'] = filter_text_input( 'ngaysinh', 'post', '', 1, 10 ); unset( $m ); if ( preg_match( "/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})$/",

Page 47: Hướng dẫn viết module cho phiên bản NukeViet 3

$data['ngaysinh'], $m ) ) { $data['ngaysinh1'] = mktime( 0, 0, 0, $m[2], $m[1], $m[3] ); } else { $data['ngaysinh1'] = ""; } $data['diachi'] = $nv_Request->get_string( 'diachi', 'post', '' ); if ( ($nv_Request->get_int( 'add', 'post', 0 ) == 1) ) { if ( $data['hoten'] == "" ) { $error = $lang_module['add_student_err_name']; } elseif ( $data['ngaysinh'] == "" ) { $error = $lang_module['add_student_err_bd']; } elseif ( $data['diachi'] == "" ) { $error = $lang_module['add_student_err_ar']; } else { $sql = "SELECT `stt` FROM `" . NV_PREFIXLANG . "_" . $module_data . "` ORDER BY stt DESC LIMIT 0,1"; $resuilt = $db->sql_query( $sql ); list ( $currentstt ) = $db->sql_fetchrow( $resuilt ); $newstt = $currentstt + 1; $query = "INSERT INTO `" . NV_PREFIXLANG . "_" . $module_data . "` ( `id`, `stt`, `hoten`, `ngaysinh`, `diachi` ) VALUES ( NULL, " . $newstt . ", " . $db->dbescape( $data['hoten'] ) . ", " . $data['ngaysinh1'] . ", " . $db->dbescape( $data['diachi'] ) . " )"; if ( $db->sql_query_insert_id( $query ) ) { $db->sql_freeresult(); Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name); die(); } else { $error = $lang_module['add_student_err_save']; }

Page 48: Hướng dẫn viết module cho phiên bản NukeViet 3

} } if( $error ) { $contents .= "<div class=\"quote\" style=\"width: 780px;\">\n <blockquote class=\"error\"> <span>".$error."</span> </blockquote> </div>\n <div class=\"clear\"> </div>"; } $contents .=" <form method=\"post\"> <table class=\"tab1\"> <thead> <tr> <td colspan=\"2\"> " . $lang_module['add_student_info'] . " </td> </tr> </thead> <tbody> <tr> <td style=\"width: 150px;\"> " . $lang_module['name'] . " </td> <td style=\"background: #eee;\"> <input name=\"hoten\" style=\"width: 470px;\" value=\"" . $data['hoten'] . "\" type=\"text\"> </td> </tr> </tbody> <tbody> <tr> <td> " . $lang_module['birthdate'] . " </td> <td> <input id=\"ngaysinh\" name=\"ngaysinh\" style=\"width: 470px;\" value=\"" . $data['ngaysinh'] . "\" type=\"text\" /> <img src=\"" . NV_BASE_SITEURL . "images/calendar.jpg\" style=\"cursor: pointer; vertical-align: middle;\" onclick=\"popCalendar.show(this, 'ngaysinh', 'dd.mm.yyyy', true);\" alt=\"\" height=\"17\" /> </td> </tr> </tbody> <tbody> <tr> <td> " . $lang_module['address'] . " </td> <td> <input name=\"diachi\" style=\"width: 470px;\" value=\"" . $data['diachi'] . "\" type=\"text\" />

Page 49: Hướng dẫn viết module cho phiên bản NukeViet 3

</td> </tr> </tbody> <tr> <td colspan=\"2\" align=\"center\" style=\"background: #eee;\">\n <input name=\"confirm\" value=\"" . $lang_module['save'] . "\" type=\"submit\">\n <input type=\"hidden\" name=\"add\" value=\"1\">\n </td>\n </tr>\n </table>\n </form>\n"; include (NV_ROOTDIR . "/includes/header.php"); echo nv_admin_theme($contents); include (NV_ROOTDIR . "/includes/footer.php"); ?>

Tương tự cho bên ngoài site ta có file vi.php: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author PHAN TAN DUNG ([email protected]) * @Language Vietnamese * @Createdate jan 1, 2011 11:30:02 AM */ if ( ! defined( 'NV_MAINFILE' ) ) { die( 'Stop!!!' ); } $lang_translator['author'] = "PHAN TAN DUNG ([email protected])"; $lang_translator['createdate'] = "01/01/2011, 15:22"; $lang_translator['copyright'] = "@Copyright (C) 2011"; $lang_translator['info'] = ""; $lang_translator['langtype'] = "lang_module"; $lang_module['stt'] = "STT"; $lang_module['name'] = "Họ và tên"; $lang_module['birthdate'] = "Ngày sinh"; $lang_module['address'] = "Địa chỉ"; ?>

và sau khi thay ta cũng có file main.php: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung * @Copyright (C) 2011 * @Createdate 26/01/2011 10:26 AM */ if ( ! defined( 'NV_IS_MOD_QUANLY' ) ) die( 'Stop!!!' );

Page 50: Hướng dẫn viết module cho phiên bản NukeViet 3

$page_title = $module_info['custom_title']; $key_words = $module_info['keywords']; $contents = ''; $contents = " <table class=\"tab1\"> <thead> <tr> <td> " . $lang_module['stt'] . " </td> <td> " . $lang_module['name'] . " </td> <td> " . $lang_module['birthdate'] . " </td> <td> " . $lang_module['address'] . " </td> </tr> </thead> <tbody>"; $allSTD = getALLstudent( ); foreach ( $allSTD as $student ) { $contents .= " <tr> <td> " . $student['stt'] . " </td> <td> " . $student['hoten'] . " </td> <td> " . $student['ngaysinh'] . " </td> <td> " . $student['diachi'] . " </td> </tr>"; } $contents .= "</tbody></table>"; include ( NV_ROOTDIR . "/includes/header.php" ); echo nv_site_theme( $contents ); include ( NV_ROOTDIR . "/includes/footer.php" ); ?>

CHúng ta vừa hoàn thành xong phần làm việc với file lang bây giờ ta tiếp tục đến với phần sử dụng xtemplate. xtemplate thực chất là một lớp (class) được xây dựng sẵn và luôn được tích hợp trong nukeviet. Để sử dụng nó ta cần quan tâm đến 3 phần như sau. -- Gọi file tpl

Page 51: Hướng dẫn viết module cho phiên bản NukeViet 3

-- lệnh $xtpl->assign -- lệnh $xtpl->parse -- lẹnh $xtpl->text( 'main' ) Để gọi ra một file tpl ta dùng như sau: Mã: Chọn tất cả

$xtpl = new XTemplate( "managersong.tpl", NV_ROOTDIR . "/themes/" . $module_info['template'] . "/modules/" . $module_file );

Ta thấy có hai phần phân cách nhau bởi dấu phảy. phần trước chính là tên của file tpl là phần sau là đường dẫn đến file tpl đó. Các giá trị hằng, biến NV_ROOTDIR, $module_info['template'], $module_file các bạn tham khảo trong file mainfile.php. Hệ thông sẽ gọi ra file tpl ở theme bạn đang dùng nếu không tồn tại file đó thì sẽ tiếp tục tìm đến file đó trong theme default, admin_default nếu không tồn tại thì sẽ báo lỗi. -> lệnh $xtpl->assign có chức năng gán giá trị cho biến trong xtpl. có thể là biến thông thường cũng có thể là mảng. -> lệnh $xtpl->parse, $xtpl->text( 'main' ) ta sẽ hiểu thông qua ví dụ cụ thể. Nhắc đến xtpl ta không thể không nhắc đến file theme.php (ngang hàng với file action.php). File này chứa tất cả cấu trúc xtpl. Nhưng không bắt buộc phải có nó, xtpl ta có thể dùng bất kì nơi nào. Để cho nhất quán, bài học này chúng ta sẽ dùng đến nó. Cấu trúc xtpl thông thường sẽ gọi các file có đuôi là tpl nhưng không nhất thiết phảo như thế, trong khi viết, để cho dễ dàng thao tác với chúng ta có thể chuyển lại chúng thành html và dùng các trình soạn thảo HTML để làm việc, sau khi hoàn tất ta sẽ chuyển trở lại thành tpl. Bây giờ ta sẽ đi vào ví dụ cụ thể. Đầu tiên là phần admin.

Tạo thư mục và file như sau: themes/admin_default/modules/quanlihs/main.tpl. Nội dung file mail.tpl: Mã: Chọn tất cả

<!-- BEGIN: main --> <table class="tab1"> <thead> <tr> <td> {LANG.stt} </td> <td> {LANG.name} </td> <td> {LANG.birthdate} </td> <td> {LANG.address}

Page 52: Hướng dẫn viết module cho phiên bản NukeViet 3

</td> </tr> </thead> <tbody> <!-- BEGIN: loop --> <tr> <td> {DATA.stt} </td> <td> {DATA.hoten} </td> <td> {DATA.ngaysinh} </td> <td> {DATA.diachi} </td> </tr> <!-- END: loop --> </tbody> </table> <!-- END: main -->

Sửa lại file main.php trong thư mục admin như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Phan Tan Dung ([email protected]) * @Copyright (C) 2011 * @Createdate 26-01-2011 14:43 */ if ( ! defined( 'NV_IS_QUANLY_ADMIN' ) ) die( 'Stop!!!' ); $page_title = $lang_module['main_page']; $allSTD = getALLstudent( ); $xtpl = new XTemplate("main.tpl", NV_ROOTDIR . "/themes/" . $global_config['module_theme'] . "/modules/" . $module_name); $xtpl->assign('LANG', $lang_module); foreach ( $allSTD as $student ) { $xtpl->assign('DATA', $student); $xtpl->parse('main.loop'); } $xtpl->parse('main'); $contents = $xtpl->text('main'); include (NV_ROOTDIR . "/includes/header.php"); echo nv_admin_theme($contents); include (NV_ROOTDIR . "/includes/footer.php"); ?>

Page 53: Hướng dẫn viết module cho phiên bản NukeViet 3

Nhìn vào hai đoạn code trên ta thấy rằng: Cấu trúc file main.tpl không khác gì một file HTML. Cần chú ý đến thẻ <!-- BEGIN: main --> và <!-- END: main --> đây là dấu hiệu cho biết bắt đầu và kết thúc của lệnh $xtpl->parse('main'); đối với những lệnh $xtpl->parse tiếp theo đặt trong main thì ta sẽ sử dụng lệnh $xtpl->parse('main.loop');. Chúng đặt nối tiếp nhau bởi dấu ".". Để trả về nội dung HTML ta sử dụng lệnh $xtpl->text('main'). Lưu ý đối với những nội dung nằm ngoài <!-- BEGIN: main --> và <!-- END: main --> chúng sẽ được bỏ qua. Lợi dụng đặc tính này ta chuyển file tpl sang html và viết tương tự như viết HTML sau khi viết xong ta sẽ cắt bỏ tất cả những phần ngoài hai thẻ này đi còn lại phần cần thiết. Đối với lệnh $xtpl->assign mà giá trị là biết đơn thì trong tpl chỉ cần gọi ra bằng {ten_bien} còn nếu là mảng thì để tham chiếu đến phần tử ta dùng {ten_bien.phan_tu}. Vừa rồi là phần admin,tiếo theo tới bên ngoài site. Bên ngoài site ta càn tạo thêm một file theme.php (không nhất thiết phải có) lưu ngang hàng với file action.php với nội dung như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @copyright 2011 * @createdate 26/01/2011 09:17 AM */ if ( ! defined( 'NV_IS_MOD_QUANLY' ) ) die( 'Stop!!!' ); function nv_quanly_main ( $data ) { global $module_file, $lang_module, $module_info; $xtpl = new XTemplate( "main.tpl", NV_ROOTDIR . "/themes/" . $module_info['template'] . "/modules/" . $module_file ); $xtpl->assign( 'LANG', $lang_module ); foreach ( $data as $st ) { $xtpl->assign( 'DATA', $st ); $xtpl->parse( 'main.loop' ); } $xtpl->parse( 'main' ); return $xtpl->text( 'main' ); } ?>

file theme.php thực chất là các function làm nhiệm vụ thực hiện thao tác với xtpl, nếu không cần file này ta có thển thục hiện ngay trong file main.php. Tiếp tục sửa lại file main.php như sau:

Page 54: Hướng dẫn viết module cho phiên bản NukeViet 3

Mã: Chọn tất cả <?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung * @Copyright (C) 2011 * @Createdate 26/01/2011 10:26 AM */ if ( ! defined( 'NV_IS_MOD_QUANLY' ) ) die( 'Stop!!!' ); $page_title = $module_info['custom_title']; $key_words = $module_info['keywords']; $allSTD = getALLstudent( ); $contents = nv_quanly_main( $allSTD ); include ( NV_ROOTDIR . "/includes/header.php" ); echo nv_site_theme( $contents ); include ( NV_ROOTDIR . "/includes/footer.php" ); ?>

Làm tới đây ta vào module quanlyhs bên ngoài sile sẽ bị lỗi như sau: [XTemplate] * [] (C:/wamp/www/nukeviet3/themes/default/modules/quanlihs\main.tpl) does not exist * parse: blockname [main.loop] does not exist * parse: blockname [main.loop] does not exist * parse: blockname [main.loop] does not exist * parse: blockname [main] does not exist Đó là lỗi không tồn tại file main.tpl. Ta cần thêm file main.tpl vào themes/theme_dang_dung/modules/quanlyhs/main.tpl: Mã: Chọn tất cả

<!-- BEGIN: main --> <table class="tab1"> <thead> <tr> <td> {LANG.stt} </td> <td> {LANG.name} </td> <td> {LANG.birthdate} </td> <td>

Page 55: Hướng dẫn viết module cho phiên bản NukeViet 3

</<!

Vừa rồi tsẵn. Nếu đặt vào thmục có tênhất thiếtgói moduTiếp theo -- Mở rộnThêm fileMã: Chọ

<? /** * * * */ if $l //liasif

</ </the <tbod <!-- BEG <tr> <t </ <t </ <t </ <t </ </tr> <!-- END </tbody>/table> !-- END: m

ta đã hoàn thmuốn tùy ch

hư mục css cên là tên củat phải đặt cáule hệ thống o ta sẽ tới ph

ng thông tin e siteinfo.phn tất cả ?php

** @Project @Author P@copyrigh@createda/

f ( ! defi

lang_sitei

/ So hoc sist( $numbs number Ff ( $numbe

{LANG. </td> /tr> ead> dy> GIN: loop -

td> {DATA.stt/td> td> {DATA.hot/td> td> {DATA.nga/td> td> {DATA.dia/td> > D: loop -->>

main -->

hành xong phhỉnh giao diệcủa theme đaa module đặtác ảnh vào th

sẽ đóng góihần mở rộng

trong adminhp đặt ngang

NUKEVIET-MPhan Tan Duht 2011 ate 26/01/2

ined( 'NV_I

info = nv_g

sinh ber ) = $dbFROM `" . Ner > 0 )

.address}

-->

t}

ten}

aysinh}

achi}

>

hần xtemplatện cho moduang dùng, mt vào thư mụhư mục như vi tất cả các ảnthêm thông

n phần "thôn hàng với fil

MUSIC ung (phant

2011 10:10

IS_FILE_SI

get_lang_m

b->sql_fetNV_PREFIXL

te. Với phầnule bạn hãy t

muốn dùng thục images củvậy. Một lý nh trong thưtin module.

ng tin từ các le action.php

tandung92@g

0 AM

ITEINFO' )

module( $mo

tchrow( $dbLANG . "_"

n như trên ta tạo file CSS

hêm phần hìnủa theme đando để đặt cáư mục này lu

module" p nội dung n

gmail.com)

) die( 'S

od );

b->sql_que. $mod_da

a chỉ đơn giảnS có tên là tênnh ảnh thì cáng dùng chứaác ảnh như vuôn.

như sau:

Stop!!!' );

ery( "SELECata . "`" )

n dùng CSS n của modulác bạn tạo tha các ảnh. K

vậy là khi đón

;

CT COUNT(*) );

có le và hư

Không ng

*)

Page 56: Hướng dẫn viết module cho phiên bản NukeViet 3

{ $siteinfo[] = array( 'key' => $lang_siteinfo['siteinfo_num_student'] , 'value' => $number ); } ?>

đoạn code này đơn giản là tính số dòng trong table => số học sinh và gán thêm bào biến $siteinfo[]. Để có các thông tin khác bạn cứ gán vô biến $siteinfo[] là được. Thêm Mã: Chọn tất cả

$lang_module['siteinfo_num_student'] = "Tổng số học sinh"; vào file admin_vi.php để có thêm file ngôn ngữ. Bây giờ các bạn vào phần quản trị sẽ thấy thêm tại Thông tin từ các module có thêm: Module Nội dung Giá trị Quản lí Tổng số học sinh 3 (tùy theo module của bạn) -- Thêm RSS cho module: Thêm file rssdata.php đặt cùng cấp với file action.php với nội dung như sau: Mã: Chọn tất cả

<?php /** * @Project DOCUMENT * @Author Phan Tan Dung ([email protected]) * @copyright 2011 * @createdate 01/02/2011 09:47 */ if ( ! defined( 'NV_IS_MOD_RSS' ) ) die( 'Stop!!!' ); $rssarray = array(); $result2 = $db->sql_query( "SELECT * FROM " . NV_PREFIXLANG . "_" . $module_data . " ORDER BY stt" ); while ( $row = $db->sql_fetchrow( $result2 ) ) { $rssarray[$row['id']] = array( 'catid' => $row['id'], 'parentid' => 0, 'title' => $row['hoten'], 'link' => NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" . NV_NAME_VARIABLE . "=" . $module_title . "&amp;" . NV_OP_VARIABLE . "=rss/" . $row['id'] ); } ?>

Code trên lấy thông tin tất cả các học sinh. Tiếp tục thêm file rss.php đặt vào thư mục funcs với nội dung : Mã: Chọn tất cả

Page 57: Hướng dẫn viết module cho phiên bản NukeViet 3

<?php /** * @Project DOCUMENT * @Author Phan Tan Dung ([email protected]) * @copyright 2011 * @createdate 01/02/2011 09:47 */ if ( ! defined( 'NV_IS_MOD_QUANLY' ) ) die( 'Stop!!!' ); $channel = array(); $items = array(); $channel['title'] = $global_config['site_name'] . ' RSS: ' . $module_info['custom_title']; $channel['link'] = NV_MY_DOMAIN . NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" . NV_NAME_VARIABLE . "=" . $module_name; $channel['atomlink'] = NV_MY_DOMAIN . NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" . NV_NAME_VARIABLE . "=" . $module_name . "&amp;" . NV_OP_VARIABLE . "=rss"; $channel['description'] = $global_config['site_description']; $id = isset( $array_op[1] ) ? $array_op[1] : 0; if ( $id > 0 ) { $sql = "SELECT * FROM `" . NV_PREFIXLANG . "_" . $module_data . "` WHERE `id`=" . $id; $result = $db->sql_query( $sql ); $row = $db->sql_fetchrow( $result ); $channel['title'] = $global_config['site_name'] . ' RSS: ' . $module_info['custom_title'] . ' - ' . c; $channel['link'] = NV_MY_DOMAIN . NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" . NV_NAME_VARIABLE . "=" . $module_name . "&amp;cat=" . $row['hoten']; $channel['description'] = $row['hoten']; } if ( $module_info['rss'] ) { $rimages = ""; $items[] = array( 'title' => $row['hoten'], 'link' => NV_MY_DOMAIN . NV_BASE_SITEURL . "index.php?" . NV_LANG_VARIABLE . "=" . NV_LANG_DATA . "&amp;" . NV_NAME_VARIABLE . "=" . $module_name, 'guid' => $module_name . '_' . $id, 'description' => $rimages . $row['diachi'], 'pubdate' => 0 ); } nv_rss_generate( $channel, $items ); die();

Page 58: Hướng dẫn viết module cho phiên bản NukeViet 3

?>

Đây chỉ là cách viết rss cơ bản, vì module này không thích hợp cho rss. Cách viết chi tiết hơn các bạn tham khảo ở module news. Làm đến đây các bạn cần phải xóa module đi và cài lại để hệ thông nhận ra là module này có chức năng RSS. Để không mất đi dữ liệu mà các bạn nhập thì các bạn đổi tên file action.php rồi cài lại sau đó lại đổi tên trở lại thành action.php. Ta vừa xog các phần mở rộng RSS, siteinfo Tiếp theo chúng ta sẽ đến phần sửa dụng javascript và ajax cho module. Nukeviet đã tích hợp sẵn cho chúng ta jquery cho nên bạn có thể dùng nó ở bất kì nơi đâu. Để dùng java bạn hãy cho nó vào file admin.js hoặc user.js. admin.js thì dùng trong admin còn user.js thì dùng ngoài site. Mình sẽ ví dụ với các bạn cách dùng trong admin còn ngoài site thì các bạn làm tương tự. Tạo thêm thư mục js ngang hàng với thư mục funcs. Thêm vào đó file admin.js Bạn cũng có thể dùng trực tiếp trong file tpl như sau: Ví dụ này mình sẽ trình bày cách dùng ajax để xóa một học sinh. Đầu tiên ta cần thêm funcs mới vào thư mục là del.php. ta cần chấp nhận thêm funcs này bằng cách mở file admin.functions.php, tại dòng thứ 14 sửa lại thành : Mã: Chọn tất cả

$allow_func = array('main', 'add', 'del'); tiếp tục thêm file del.php với nội dung như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @Copyright (C) 2011 * @Createdate 26/01/2011 09:05 AM */ if(!defined('NV_IS_QUANLY_ADMIN')) { die('Stop!!!'); } $result = false; $id = $nv_Request->get_int('id', 'post,get', 0); if( $id > 0 ) { $sql = "DELETE FROM `" . NV_PREFIXLANG . "_" . $module_data ."` WHERE `id`=" . $id; $result = $db->sql_query( $sql ); } if( $result ) { echo $lang_module['del_success']; } else { echo $lang_module['del_error'];

Page 59: Hướng dẫn viết module cho phiên bản NukeViet 3

} ?>

funcs này rất đơn giản là lấy id được gửi tới và xóa đi hoc sinh có id này. Nếu xóa thành công thì xuất ra $lang_module['del_success'] ngược lại thì xuất $lang_module['del_error'] Ta thêm hai biến ngôn ngữ mới này cho file ngôn ngữ đồng thời thêm tiếp các biến ngôn ngữ cần dùng về sau. Mở file admin_vi.php thêm vào: Mã: Chọn tất cả

$lang_module['del_success'] = "Xóa thành công"; $lang_module['del_error'] = "Xóa thất bại"; $lang_module['del'] = "Xóa"; $lang_module['del_cofirm'] = "Bạn có chắc chắn muốn xóa không?";

Tiếp theo ta khởi tạo link xóa một học sinh bằng cách mở file main.php thêm vào dòng thứ 19: Mã: Chọn tất cả

$xtpl->assign('URL_DEL', "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&" . NV_OP_VARIABLE . "=del&id="); $xtpl->assign('URL_DEL_BACK', "index.php?" . NV_NAME_VARIABLE . "=" . $module_name );

Tiếp tục mở file main.php (admin) lên sửa lại thành: Mã: Chọn tất cả

<!-- BEGIN: main --> <table class="tab1"> <thead> <tr> <td> {LANG.stt} </td> <td> {LANG.name} </td> <td> {LANG.birthdate} </td> <td> {LANG.address} </td> <td></td> </tr> </thead> <tbody> <!-- BEGIN: loop --> <tr> <td> {DATA.stt} </td> <td> {DATA.hoten} </td> <td> {DATA.ngaysinh} </td>

Page 60: Hướng dẫn viết module cho phiên bản NukeViet 3

<td> {DATA.diachi} </td> <td align="center"> <span class="delete_icon"> <a class='delfile' href="{URL_DEL}{DATA.id}">{LANG.del}</a> </span> </td> </tr> <!-- END: loop --> </tbody> </table> <script type='text/javascript'> $(function() { $('a[class="delfile"]').click(function(event) { event.preventDefault(); if (confirm("{LANG.del_cofirm}")) { var href = $(this).attr('href'); $.ajax( { type: 'POST', url: href, data: '', success: function(data) { alert(data); window.location = '{URL_DEL_BACK}'; } }); } }); }); </script> <!-- END: main -->

Nhìn vào hai doạn code trên ta thấy phần function getALLstudent( ) thiếu mất lấy ra id học sinh ta lại mỏ file global.functions.php lên sửa lại thành: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @copyright 2011 * @createdate 26/01/2011 09:17 AM */ if ( ! defined( 'NV_MAINFILE' ) ) die( 'Stop!!!' ); function getALLstudent( ) {

Page 61: Hướng dẫn viết module cho phiên bản NukeViet 3

global $module_data, $db; $data = array() ; $result = $db->sql_query( "SELECT `id`, `stt`, `hoten`, `ngaysinh`, `diachi` FROM " . NV_PREFIXLANG . "_" . $module_data . " ORDER BY stt ASC" ); while ( list ( $id, $stt, $hoten, $ngaysinh, $diachi ) = $db->sql_fetchrow($result) ) { $data[] = array ( "id" => $id, "stt" => $stt, "hoten" => $hoten, "ngaysinh" => date ( "d/m/Y", $ngaysinh ), "diachi" => $diachi ); } return $data ; } ?>

Như vậy ta đã hoàn thành việc sử dụng ajax cho module Tiếp theo ta sẽ đến với một phần nâng cao hơn: Sử dụng các lớp (class) có sẵn. Thư mục chứa class Class không có gì là quá phức tạp, nó thực chất ra là một tập hợp nhiều function liê kết với nhau để làm một công liệc cụ thể nào đó. Hiện tại nukeviet3.1 có các class tích hợp sẵn như sau: - array2xml.class.php: xử lí file xml : đọc, xuất.... - download.class.php: thao tác trong việc download file: - error.class.php ... tất cả trong thư mục includes/class Mình chỉ hướng dẫn các bạn dùng một số class thường dùng thôi. Đầu tiên là : download.class.php. NẾu mở file này lên bạn sẽ thấy đoạn này: Mã: Chọn tất cả

/** * include("download.class.php"); // load the class file * $fichier = new download("example.zip"); // use the original file name, disallow resuming, no speed limit * $fichier = new download("example.zip","My Example.zip") ; // rename the file, disallow resuming, no speed limit * $fichier = new download("example.zip","My Example.zip",true) ; // rename the file, allow resuming, no speed limit * $fichier = new download("example.zip","My Example.zip",true,80) ; // rename the file, allow resuming, speed limit 80ko/s

Page 62: Hướng dẫn viết module cho phiên bản NukeViet 3

* */

Đây chín- Gọi ra c- Nhập cá- Gọi ra f Ví dụ để - Gọi ra cMã: Chọ

re - Nhập thMã: Chọ

$d"/

- và downTrong ph* $fichiespeed lim* $fichieresuming* $fichieresuming* $fichieresumingChịu khó DÙNG CCác bạn mMã: Chọ

/** * * $m* * * * * * * * * *

$fichier-/

nh là phần hưclass. Bạn cóác thông số kfunction thự

download mclass downlon tất cả equire_onc

hông số: n tất cả download =/demo.zip"

n về $downlhần nhập thôr = new dow

mit r = new dow

g, no speed lr = new dow

g, no speed lr = new dow

g, speed limió đọc tiếng a

CLASS imagmở file này n tất cả ** if(!file_@require_$image = max_width,$image->r$image->c$image->c$image->a$image->a$image->r$image->r$image->r$image->s$image->s

->download_

ướng dẫn dùó thể dùng inkhởi tạo c hiên.

một file demooad:

ce ( NV_ROO

= new downl", NV_ROOTD

load->downlông sô các bạwnload("exam

wnload("examimit

wnload("examimit

wnload("examit 80ko/s anh xí nhé

ge.class.php lê cũng sẽ th

_exists(NV__once(NV_ROnew image($max_heighresizePercecropFromCencropFromLefaddstring("addlogo(NV_resizePercerotate(45);reflection(show(); save(NV_ROO

_file();

ng nó. Dùngnclude hoặc

o.zip ta làm

OTDIR . '/

load( NV_RDIR . "/"

load_file();ạn chú ý nhưmple.zip"); /

mple.zip","M

mple.zip","M

mple.zip","M

hấy hướng d

_ROOTDIR."OOTDIR."/i(NV_ROOTDIht); ent(200);nter(150,2ft(50,50,3"nguyenanh_ROOTDIR.'ent(30); ; ();

OTDIR.'/'.

g class nào crequire_onc

như sau:

/includes/c

ROOTDIR . ". NV_UPLOA

ư phần hướng// use the ori

My Example

My Example

My Example

dẫn

"/uploads/1includes/clIR."/images

200); 300,300);h tu", 'rig/images/lo

NV_TEMP_DI

các bạn cũngce

class/down

"/" . NV_UADS_DIR ,

g dẫn của cliginal file na

e.zip") ; // ren

e.zip",true) ;

e.zip",true,80

1237974658lass/images/logo.png

ght', 'botogo.png','

IR.'/');

g thực hiện c

nload.class

UPLOADS_DIR"demo.zip"

ass: ame, disallow

name the file

// rename th

0) ; // rename

8.jpg")) { e.class.phpg",

ttom', "", left','top

ác bước như

s.php' );

R . ", true);

w resuming,

e, disallow

he file, allow

e the file, all

p");

8); p');

ư sau:

no

w

low

Page 63: Hướng dẫn viết module cho phiên bản NukeViet 3

* * * * */

Cách dùnđầu tiên kif(!file_enếu tồn tNhập thô$max_wiTiếp theo* $image* $image* $image* $imagenguyenannguyena* $image* $image* $image* $image* $image* $image* $image* print_r( Tương tự Tiếo theofile. Ta smột trườnMã: Chọ

<? /** * * * */ if $s

$image->cprint_r($exit; } /

ng cũng tươnkiểm tra xemxists(NV_Rại thì gọi ra

ông sô : $imaidth,$max_ho là phần thae->resizePerce->cropFrome->cropFrome->addstringnhtu. HIHI B

anh tu e->addlogo(Ne->resizePerce->rotate(45)e->reflectione->show(); e->save(NV_e->close(); ($image->cr

ự với các các

o mình sẽ hưẽ dùng chứcng nữa là 'avn tất cả ?php

** @Project @Author P@copyrigh@createda/

f( !define

sql_drop_m

close(); $image->cre

ng tự: m file tồn tại

ROOTDIR."/uclass @requage = new imheight); gồmao tác: cent(200); C

mCenter(150mLeft(50,50,

("nguyenanhBÁC ANH T

NV_ROOTDcent(30); ); xoay ảnh 4

n();

_ROOTDIR

reate_Image_

ch như trên c

ướng dẫn cácc năng trên đvatar'. Ta mở

NUKEVIET-MPhan Tan Duht 2011 ate 26/01/2

ed('NV_IS_F

module = ar

eate_Image

hay không: uploads/123

uire_once(NVmage(NV_R

m đường dẫn

Chỉnh sửa kíc,200); Cắt ản300,300); Ch tu", 'right',TU CHƠI GH

DIR.'/images

45 độ

R.'/'.NV_TEM

_info);

các bạn có th

c bạn dùng mđể lưu ảnh củở file action.

MUSIC ung (phant

2011 10:10

FILE_MODUL

rray();

e_info);

37974658.jpgV_ROOTDI

ROOTDIR."/tới file ảnh,

ch thước thenh từ chính gắt từ bên trá 'bottom', ""HÊ QUÁ. cl

s/logo.png','l

MP_DIR.'/');

hể dùng bất k

một phần tíchủa học sinh. php lên sửa

tandung92@g

0 AM

LES') ) die

g")) { IR."/includes/images/logochiều rộng,

eo thông sô pgiữa

ái , ; Thêm vlass do bác v

left','top'); T

Lưu ảnh lại

kì class nào.

h hợp nữa. ĐTa sẽ tạo thêlại:

gmail.com)

e('Stop!!!

s/class/imago.png", cao tối đa.

phần trăm

vào ảnh dònviết nên chơi

Thêm vào ảnh

i

.

Đó là phần pêm vào table

');

ge.class.php"

ng chữ i luôn string

h logo

popup để uple của module

");

oad e

Page 64: Hướng dẫn viết module cho phiên bản NukeViet 3

$sql_drop_module[] = "DROP TABLE IF EXISTS `" . $db_config['prefix'] . "_" . $lang . "_" . $module_data . "`"; $sql_create_module = $sql_drop_module; $sql_create_module[] = "CREATE TABLE `" . $db_config['prefix'] . "_" . $lang . "_" . $module_data . "` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `stt` INT( 255 ) unsigned NOT NULL, `hoten` varchar(255) NOT NULL, `ngaysinh` INT( 11 ) NOT NULL DEFAULT '0', `diachi` varchar(255) NOT NULL, `avatar` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM"; ?>

SAu khi thêm vào file action.php các bạn cài lại module thì table sẽ được thêm một trường nữa. Ta chỉnh lại file add.php như sau: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Phan Tan Dung ([email protected]) * @Copyright (C) 2011 * @Createdate 26-01-2011 14:43 */ if ( ! defined( 'NV_IS_QUANLY_ADMIN' ) ) die( 'Stop!!!' ); $page_title = $lang_module['add_student']; $my_head = "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/popcalendar/popcalendar.js\"></script>\n"; $my_head .= "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.js\"></script>\n"; $my_head .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.css\" />\n"; $my_head .= "<script type=\"text/javascript\">\n"; $my_head .= "Shadowbox.init({\n"; $my_head .= "});\n"; $my_head .= "</script>\n"; $contents = ""; $error = ""; $data = array(); $data['hoten'] = filter_text_input( 'hoten', 'post', '' ); $data['ngaysinh'] = filter_text_input( 'ngaysinh', 'post', '', 1, 10 ); unset( $m ); if ( preg_match( "/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})$/", $data['ngaysinh'], $m ) ) { $data['ngaysinh1'] = mktime( 0, 0, 0, $m[2], $m[1], $m[3] ); } else

Page 65: Hướng dẫn viết module cho phiên bản NukeViet 3

{ $data['ngaysinh1'] = ""; } $data['diachi'] = $nv_Request->get_string( 'diachi', 'post', '' ); $data['avatar'] = $nv_Request->get_string( 'avatar', 'post', '' ); if ( ($nv_Request->get_int( 'add', 'post', 0 ) == 1) ) { if ( $data['hoten'] == "" ) { $error = $lang_module['add_student_err_name']; } elseif ( $data['ngaysinh'] == "" ) { $error = $lang_module['add_student_err_bd']; } elseif ( $data['diachi'] == "" ) { $error = $lang_module['add_student_err_ar']; } else { $sql = "SELECT `stt` FROM `" . NV_PREFIXLANG . "_" . $module_data . "` ORDER BY stt DESC LIMIT 0,1"; $resuilt = $db->sql_query( $sql ); list ( $currentstt ) = $db->sql_fetchrow( $resuilt ); $newstt = $currentstt + 1; $query = "INSERT INTO `" . NV_PREFIXLANG . "_" . $module_data . "` ( `id`, `stt`, `hoten`, `ngaysinh`, `diachi`, `avatar` ) VALUES ( NULL, " . $newstt . ", " . $db->dbescape( $data['hoten'] ) . ", " . $data['ngaysinh1'] . ", " . $db->dbescape( $data['diachi'] ) . ", " . $db->dbescape( $data['avatar'] ) . " )"; if ( $db->sql_query_insert_id( $query ) ) { $db->sql_freeresult(); Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name); die(); } else { $error = $lang_module['add_student_err_save']; } } }

Page 66: Hướng dẫn viết module cho phiên bản NukeViet 3

if( $error ) { $contents .= "<div class=\"quote\" style=\"width: 780px;\">\n <blockquote class=\"error\"> <span>".$error."</span> </blockquote> </div>\n <div class=\"clear\"> </div>"; } $contents .=" <form method=\"post\"> <table class=\"tab1\"> <thead> <tr> <td colspan=\"2\"> " . $lang_module['add_student_info'] . " </td> </tr> </thead> <tbody> <tr> <td style=\"width: 150px;\"> " . $lang_module['name'] . " </td> <td style=\"background: #eee;\"> <input name=\"hoten\" style=\"width: 470px;\" value=\"" . $data['hoten'] . "\" type=\"text\"> </td> </tr> </tbody> <tbody> <tr> <td> " . $lang_module['birthdate'] . " </td> <td> <input id=\"ngaysinh\" name=\"ngaysinh\" style=\"width: 470px;\" value=\"" . $data['ngaysinh'] . "\" type=\"text\" /> <img src=\"" . NV_BASE_SITEURL . "images/calendar.jpg\" style=\"cursor: pointer; vertical-align: middle;\" onclick=\"popCalendar.show(this, 'ngaysinh', 'dd.mm.yyyy', true);\" alt=\"\" height=\"17\" /> </td> </tr> </tbody> <tbody> <tr> <td> " . $lang_module['address'] . " </td> <td> <input name=\"diachi\" style=\"width: 470px;\" value=\"" . $data['diachi'] . "\" type=\"text\" /> </td> </tr> </tbody>

Page 67: Hướng dẫn viết module cho phiên bản NukeViet 3

<tbody> <tr> <td> " . $lang_module['avatar'] . " </td> <td> <input name=\"avatar\" id=\"avatar\" style=\"width: 470px;\" value=\"" . $data['avatar'] . "\" type=\"text\" /> <input name=\"select\" type=\"button\" value=\"".$lang_module['select']."\" /> </td> <script type=\"text/javascript\"> $(\"input[name=select]\").click(function() { var area = \"avatar\"; // return value area var path = \"".NV_UPLOADS_DIR . "/" . $module_name."\"; nv_open_browse_file(\"".NV_BASE_ADMINURL . 'index.php?' . NV_NAME_VARIABLE . '=upload&popup=1&area=" + area+"&path="+path, "NVImg", "850", "500","resizable=no,scrollbars=no,toolbar=no,location=no,status=no'."\"); return false; }); </script> </tr> </tbody> <tr> <td colspan=\"2\" align=\"center\" style=\"background: #eee;\">\n <input name=\"confirm\" value=\"" . $lang_module['save'] . "\" type=\"submit\">\n <input type=\"hidden\" name=\"add\" value=\"1\">\n </td>\n </tr>\n </table>\n </form>\n"; include (NV_ROOTDIR . "/includes/header.php"); echo nv_admin_theme($contents); include (NV_ROOTDIR . "/includes/footer.php"); ?>

ta thêm tiếp vào file admin_vi.php các biến lang: Mã: Chọn tất cả

$lang_module['avatar'] = "Ảnh hiển thị"; $lang_module['select'] = "Chọn";

Trong phần thêm thêm của file add.php các bạn thấy đoạn code sau: Mã: Chọn tất cả

<td> <input name=\"avatar\" id=\"avatar\" style=\"width: 470px;\" value=\"" . $data['avatar'] . "\" type=\"text\" /> <input name=\"select\" type=\"button\" value=\"".$lang_module['select']."\" />

Page 68: Hướng dẫn viết module cho phiên bản NukeViet 3

</td> <script type=\"text/javascript\"> $(\"input[name=select]\").click(function() { var area = \"avatar\"; // return value area var path = \"".NV_UPLOADS_DIR . "/" . $module_name."\"; nv_open_browse_file(\"".NV_BASE_ADMINURL . 'index.php?' . NV_NAME_VARIABLE . '=upload&popup=1&area=" + area+"&path="+path, "NVImg", "850", "500","resizable=no,scrollbars=no,toolbar=no,location=no,status=no'."\"); return false; }); </script>

Đoạn code này thường không thay đổi nhiều. Bạn chỉ cần thay đổi các thông số sau: - input[name=select]: Đây là name của input mà khi bạn ấn vào thì hệ thông sẽ mở popup - var path = \"".NV_UPLOADS_DIR . "/" . $module_name."\"; đường dẫn đến thư mục upload - var area = \"avatar\"; ID của một input mà giá trị đường dẫn của ảnh vừa upload sẽ trả về. Tiếo theo mình sẽ hướng dẫn các bạn tiếp tục dùng một phần nữa đó là dùng trìn soạn thảo CKedittor. Để dùng các bạn làm như sau: Tại file php bạn cần dùng thêm vào: Mã: Chọn tất cả

if ( defined( 'NV_EDITOR' ) ) { require_once ( NV_ROOTDIR . '/' . NV_EDITORSDIR . '/' . NV_EDITOR . '/nv.php' ); }

đoạn code trên có nghĩa là nếu mà cho phép dùng edittor thì sẽ gọi ra file nv.php. Nếu các bạn mạnh tay hơn thì có thể gọi ra luôn mặc dù không cho hay cho phép editor. Tiếp theo thêm đoạn code này: Mã: Chọn tất cả

if ( defined( 'NV_EDITOR' ) and function_exists( 'nv_aleditor' ) ) { $contents .= nv_aleditor( 'describe', '680px', '250px', $albumdata['describe'] ); } else { $contents .= "<textarea style=\"width: 680px\" value=\"".$albumdata['describe']."\" name=\"describe\" id=\"describe\" cols=\"20\" rows=\"15\"></textarea>\n"; }

TA cần chú ý đoạn code trên ở chỗ: nv_aleditor( 'describe', '680px', '250px', $albumdata['describe'] );

Page 69: Hướng dẫn viết module cho phiên bản NukeViet 3

thông số đầu tiên describe là name của editor, 680px', '250px chiều rộng, cao $albumdata['describe']: là nội dung ban đầu trong edittor Tiếp theo mình sẽ hướng dẫn các bạn dùng shadowbox có sẵn trong nukeviet Ta sẽ dùng nó để hiển thị ảnh của học sinh như đã thêm ở phần trên. Đầu tiên ta sẽ chỉnh lại file global.functions.php để nó lấy thêm avatar của học sinh: Mã: Chọn tất cả

<?php /** * @Project NUKEVIET-MUSIC * @Author Phan Tan Dung ([email protected]) * @copyright 2011 * @createdate 26/01/2011 09:17 AM */ if ( ! defined( 'NV_MAINFILE' ) ) die( 'Stop!!!' ); function getALLstudent( ) { global $module_data, $db; $data = array() ; $result = $db->sql_query( "SELECT `id`, `stt`, `hoten`, `ngaysinh`, `diachi`, `avatar` FROM " . NV_PREFIXLANG . "_" . $module_data . " ORDER BY stt ASC" ); while ( list ( $id, $stt, $hoten, $ngaysinh, $diachi, $avatar ) = $db->sql_fetchrow($result) ) { $data[] = array ( "id" => $id, "stt" => $stt, "hoten" => $hoten, "ngaysinh" => date ( "d/m/Y", $ngaysinh ), "diachi" => $diachi, "avatar" => $avatar ); } return $data ; } ?>

tiếo theo chỉnh lại file main.php trong thư mục funcs: Thêm vào dòng 12 đoạn code sau: Mã: Chọn tất cả

if ( ! defined( 'SHADOWBOX' ) ) { global $my_head; $my_head .= "<link rel=\"Stylesheet\" href=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.css\" />\n"; $my_head .= "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.js\"></script>\n"; $my_head .= "<script type=\"text/javascript\">Shadowbox.init();</script>"; define( 'SHADOWBOX', true ); }

Page 70: Hướng dẫn viết module cho phiên bản NukeViet 3

ta thấy xuất hiện biến $my_head biến này sẽ thêm java, css, meta vào thẻ <head></head>. Đoạn code trên chính là gọi ra shadowbox. Để dùng nó ta tiếp tục chỉnh file main.tpl (ngàoi site) như sau: Mã: Chọn tất cả

<!-- BEGIN: main --> <table class="tab1"> <thead> <tr> <td> {LANG.stt} </td> <td> {LANG.name} </td> <td> {LANG.birthdate} </td> <td> {LANG.address} </td> <td> {LANG.avatar} </td> </tr> </thead> <tbody> <!-- BEGIN: loop --> <tr> <td> {DATA.stt} </td> <td> {DATA.hoten} </td> <td> {DATA.ngaysinh} </td> <td> {DATA.diachi} </td> <td> <a href="{DATA.avatar}" title="{DATA.hoten}" rel="shadowbox[miss]"><img alt="" width="100" src="{DATA.avatar}" boder="0" height="80" /></a> </td> </tr> <!-- END: loop --> </tbody> </table> <!-- END: main -->

THêm vào file vi.php biến lang: Mã: Chọn tất cả

$lang_module['avatar'] = "Ảnh";

Page 71: Hướng dẫn viết module cho phiên bản NukeViet 3

Trong phần thêm file main.tpl ta cần chú ý tới: Mã: Chọn tất cả

<a href="{DATA.avatar}" title="{DATA.hoten}" rel="shadowbox[miss]"><img alt="" width="100" src="{DATA.avatar}" boder="0" height="80" /></a>

Thẻ <a> ở đây chính là Shadowbox. Khi đã gọi ra Shadowbox rồi thì chỉ cần thêm thẻ <a> tương tụ trên là ta có thể dùng rồi. Phần cuối cùng mình sẽ hướng dẫn với các bạn cách thêm các phần mở rộng không có sẵn trong nukeviet. Ví dụ ở đây chúng ta vừa làm Shadowbox nhưng chúng ta thấy nó chưa đẹp, ta thích cái khác đẹp hơn. Ví dụ mình có cái divbox divbox.rar Ta tạo thư mục divbox đặt ngang hàng với thư mục admin của module. giải nén cho hết các file, thư mục vào đó. Để dùng nó ta làm như sau: Mở file main.php lên thay lại đoạn: Mã: Chọn tất cả

if ( ! defined( 'SHADOWBOX' ) ) { global $my_head; $my_head .= "<link rel=\"Stylesheet\" href=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.css\" />\n"; $my_head .= "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.js\"></script>\n"; $my_head .= "<script type=\"text/javascript\">Shadowbox.init();</script>"; define( 'SHADOWBOX', true ); }

Bằng đoạn: Mã: Chọn tất cả

global $my_head; $my_head .= "<link rel=\"stylesheet\" href=\"" . NV_BASE_SITEURL . "modules/" . $module_name . "/divbox/css/divbox.css\" type=\"text/css\" />\n"; $my_head .= "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "modules/" . $module_name . "/divbox/divbox.js\"></script>\n";

Tiếp tục mở file main.tpl lên thay đoạn: Mã: Chọn tất cả

<a href="{DATA.avatar}" title="{DATA.hoten}" rel="shadowbox[miss]"><img alt="" width="100" src="{DATA.avatar}" boder="0" height="80" /></a>

Thành Mã: Chọn tất cả

<a href="{DATA.avatar}" title="{DATA.hoten}" class="lightbox"><img alt="" width="100" src="{DATA.avatar}" boder="0" height="80" /></a>

Page 72: Hướng dẫn viết module cho phiên bản NukeViet 3

tiếp tục thMã: Chọ

<s </

tại dòng Như vậy hoàn thàn Như vậy phức tạp P/S: một thư mục

hêm vào filen tất cả script typ $('a.lig/script>

cuối dùng trta đã tích hợ

nh để xem k

mình vừa gi. Hy vọng vớ

khâu quan tcủa module

e đang mở đo

pe="text/jaghtbox').di

rên <!-- ENDợp thành côn

kết quả nào!!

iới thiệu vớiới bài viết nà

trọng nữa, đểfile index.ht

oạn

avascript"ivbox();

D: main -->.ng divbox ch

i các bạn taoày các bạn c

ể bảo mật chtml và file .h

">

ho module củ

on bộ công đó thể thực h

ho hệ thông chtaccess

ủa mình. Bâ

oạn viết mộthiện được mo

các bạn đừn

ây giờ vô lại

t module từ odule theo ý

ng quên thêm

module bạn

đơn giản đếmuốn của m

m vào tất cả c

vừa

n mình.

các