Mục lục

Lập trình Modules cho NukeViet 4.x

Tổng quan về Module của NukeViet

Các module của NukeViet được đặt trong thư mục modules/. Tên mỗi module bao gồm chữ cái, chữ số và dấu gạch ngang. Cấu trúc cơ bản của một module bao gồm các file và thư mục:

Chức năng của các file như sau:

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=qlhs&op=main

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à main). Giá trị op này chính là tên của funcs trong thư mục funcs hay admin. Nếu trên url mà khuyết phần op= có nghĩa funcs main.php đang được chạy.

Danh sách các Biến hằng số trong module

Quy tắc đặt tên phiên bản module

Quy tắc đặt tên phiên bản module tương tự quy tắc đặt tên phiên bản chính của NukeViet (nhưng không liên quan đến phiên bản NukeViet đang có). Theo quy tắc này, phiên bản của module có dạng: X.Y.ZZ

Trong đó:

Ví dụ:

Sai quy tắc Đúng quy tắc
1.0.1 1.0.01
1.0 1.0.00 hoặc 0.1.00
1 1.0.00 hoặc 0.1.00 hoặc 0.0.01

Viết module đơn giản

Để viết một module đơn giản việc đầu tiên là tạo thư mục trong thư mục modules/ sau đó tiếp tục tạo các file và thư mục bên trong như sau:

version.php

version.php
<?php
 
/**
 * @Project Module Nukeviet 4.x
 * @Author Webvang.vn (hoang.nguyen@webvang.vn)
 * @copyright 2014 J&A.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @createdate 08/10/2014 09:47
 */
 
if (!defined('NV_ADMIN') or !defined('NV_MAINFILE'))
    die('Stop!!!');
 
$module_version = array(
    'name' => 'QLHS', // Tieu de module
    'modfuncs' => 'main,viewcat,topic,groups,detail,search,content,tag,rss', // Cac function co block
    'change_alias' => 'topic,groups,content,rss',
    'submenu' => 'content,rss,search',
    'is_sysmod' => 0, // 1:0 => Co phai la module he thong hay khong
    'virtual' => 1, // 1:0 => Co cho phep ao hao module hay khong
    'version' => '4.0.00', // Phien ban cua modle
    'date' => 'Wed, 8 Oct 2014 00:00:00 GMT', // Ngay phat hanh phien ban
    'author' => 'Webvang.vn (hoang.nguyen@webvang.vn)', // Tac gia
    'note' => '', // Ghi chu
    'uploads_dir' => array(
        $module_name,
        $module_name . '/source',
        $module_name . '/temp_pic',
        $module_name . '/topics'
    ),
    'files_dir' => array($module_name . '/topics')
);

Trong đó:

admin.functions.php

admin.functions.php
<?php
 
/**
 * @Project Module Nukeviet 4.x
 * @Author Webvang.vn (hoang.nguyen@webvang.vn)
 * @copyright 2014 J&A.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @createdate 08/10/2014 09:47
 */
 
if (!defined('NV_ADMIN') or !defined('NV_MAINFILE') or !defined('NV_IS_MODADMIN'))
    die('Stop!!!');
 
define('NV_IS_QLHS_ADMIN', true);

Trong đó cần chú ý đến : define('NV_IS_QLHS_ADMIN', true) : Khai báo biến hằng xác nhận file func của module trong admin.

admin.menu.php

admin.menu.php
<?php
 
/**
 * @Project Module Nukeviet 4.x
 * @Author Webvang.vn (hoang.nguyen@webvang.vn)
 * @copyright 2014 J&A.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @createdate 08/10/2014 09:47
 */
 
if (!defined('NV_ADMIN'))
    die('Stop!!!');
 
$submenu['tags'] = $lang_module['tags'];
$submenu['groups'] = $lang_module['block'];
$submenu['topics'] = $lang_module['topics'];
$submenu['sources'] = $lang_module['sources'];
$submenu['setting'] = $lang_module['setting'];
$allow_func[] = 'topicsnews';
$allow_func[] = 'topics';
$allow_func[] = 'topicdelnews';
$allow_func[] = 'addtotopics';
$allow_func[] = 'change_topic';
$allow_func[] = 'list_topic';
$allow_func[] = 'del_topic';
$allow_func[] = 'sources';
$allow_func[] = 'change_source';
$allow_func[] = 'list_source';
$allow_func[] = 'del_source';
$allow_func[] = 'block';
$allow_func[] = 'groups';
$allow_func[] = 'del_block_cat';
$allow_func[] = 'list_block_cat';
$allow_func[] = 'chang_block_cat';
$allow_func[] = 'change_block';
$allow_func[] = 'list_block';
$allow_func[] = 'tags';
$allow_func[] = 'setting';
$allow_func[] = 'tools';

$submenu : là danh sách các menu con của module

$allow_func : xác nhận các file func của module được phép hoạt động

functions.php

functions.php
<?php
 
/**
 * @Project Module Nukeviet 4.x
 * @Author Webvang.vn (hoang.nguyen@webvang.vn)
 * @copyright 2014 J&A.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @createdate 08/10/2014 09:47
 */
 
if (!defined('NV_SYSTEM'))
    die('Stop!!!');
 
define('NV_IS_MOD_QLHS', true);

File này thường chứa các hàm sử dụng bên ngoài site, nếu module không có dùng các hàm, có thể để trống file này (như trên), tuy nhiên không được xóa nó.

Thư mục language chứa các file ngôn ngữ. Các file ngôn ngữ trong admin có dạng admin_ngon-ngu.php, các file ngôn ngữ ngoài site có dạng ngon-ngu.php.

Cấu trúc cơ bản của file ngôn ngữ:

admin_ngon-ngu.php

admin_ngon-ngu.php
<?php
 
/**
 * @Project Module Nukeviet 4.x
 * @Author Webvang.vn (hoang.nguyen@webvang.vn)
 * @copyright 2014 J&A.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @createdate 08/10/2014 09:47
 */
 
 
if (!defined('NV_ADMIN') or !defined('NV_MAINFILE'))
    die('Stop!!!');
 
$lang_translator['author'] = 'webvang.vn (hoang.nguyen@webvang.vn)';
$lang_translator['createdate'] = '08/10/2014, 15:22';
$lang_translator['copyright'] = '@Copyright (C) 2014 J&A.,JSC. All rights reserved';
$lang_translator['info'] = '';
$lang_translator['langtype'] = 'lang_module';
 
$lang_module['main'] = 'Quản lý học sinh;

ngon-ngu.php

ngon-ngu.php
<?php
 
/**
 * @Project Module Nukeviet 4.x
 * @Author Webvang.vn (hoang.nguyen@webvang.vn)
 * @copyright 2014 J&A.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @createdate 08/10/2014 09:47
 */
 
 
if (!defined('NV_MAINFILE'))
    die('Stop!!!');
 
$lang_translator['author'] = 'J&A.,JSC (hoang.nguyen@webvang.vn)';
$lang_translator['createdate'] = '08/10/2014, 15:22';
$lang_translator['copyright'] = '@Copyright (C) 2010 J&A.,JSC. All rights reserved';
$lang_translator['info'] = '';
$lang_translator['langtype'] = 'lang_module';
 
$lang_module['nocatpage'] = 'Error:No topic';

Biến $lang_module là biến lưu ngôn ngữ của module.

Thư mục js gồm hai file user.js và admin.js. File user.js chứa các câu lệnh javascript (nếu có) ngoài site, admin.js chứa javascript (nếu có) trong admin.

main.php

Lưu ý: Để xuất nội dung của module và kết thúc ta thực hiện thao tác:

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

biến $contents là toàn bộ nội dung hiển thị của module.

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

biến $contents là toàn bộ nội dung hiển thị của module.

Viết module nâng cao

Tạo CSDL ban đầu của module

thêm file action_mysql.php ngang hàng với file version.php của module

action_mysql.php
<?php
 
/**
 * @Project Module Nukeviet 4.x
 * @Author Webvang.vn (hoang.nguyen@webvang.vn)
 * @copyright 2014 J&A.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @createdate 08/10/2014 09:47
 */
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 . "_quessions";
 
$sql_create_module = $sql_drop_module;
$sql_create_module[] = "CREATE TABLE " . $db_config['prefix'] . "_" . $lang . "_" . $module_data . "_quessions (
id MEDIUMINT( 8 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
title VARCHAR( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
quession MEDIUMTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
anwser MEDIUMTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL
)ENGINE=MyISAM  DEFAULT CHARSET=utf8";

Cần chú ý đến biến $sql_drop_module$sql_create_module. Biến $sql_drop_module sẽ được dùng khi xóa một module và biến $sql_create_module sẽ được dùng khi cài lại module và thiết lập module mới.

Lưu ý : trong file action_mysql.php, khi thiết kế CSDL tránh các khóa này của mysql:

add, all, alter, analyze, and, as, asc, before, between, bigint, binary, both, by, call, cascade, case, change, 
 
char, character, check, collate, column, comment, condition, constraint, continue, convert, create, cross, 
 
current_user, cursor, database, databases, date, day_hour, day_minute, day_second, dec, decimal, declare, 
 
default, delayed, delete, desc, describe, distinct, distinctrow, drop, dual, else, elseif, enclosed, escaped, 
 
exists, exit, explain, false, fetch, file, float4, float8, for, force, foreign, from, fulltext, get, grant, 
 
group, having, high_priority, hour_minute, hour_second, identified, if, ignore, ignore_server_ids, in, index, 
 
infile, inner, insert, int1, int2, int3, int4, int8, integer, interval, into, is, iterate, join, key, keys, 
 
kill, leading, leave, left, level, like, limit, lines, load, lock, long, loop, low_priority, master_bind, 
 
master_heartbeat_period, master_ssl_verify_server_cert, match, middleint, minute_second, mod, mode, modify, 
 
natural, no_write_to_binlog, not, null, number, numeric, on, optimize, option, optionally, or, order, outer, 
 
outfile, partition, precision, primary, privileges, procedure, public, purge, read, real, references, release, 
 
rename, repeat, replace, require, resignal, restrict, return, revoke, right, rlike, rows, schema, schemas, 
 
select, separator, session, set, share, show, signal, spatial, sql_after_gtids, sql_before_gtids, 
 
sql_big_result, sql_calc_found_rows, sql_small_result, sqlstate, ssl, start, starting, straight_join, table, 
 
terminated, then, to, trailing, trigger, true, undo, union, unique, unlock, unsigned, update, usage, use, user, 
 
using, values, varcharacter, varying, view, when, where, while, with, write, year_month, zerofill

Giao diện trong Admin

Để tách html và php ra thì cần khai báo file html (*.tpl) trong thư mục themes/' . $global_config['module_theme'] . '/modules/' . $module_file

vd: func admin/main.php

main.php
<?php
 
/**
 * @Project Module Nukeviet 4.x
 * @Author Webvang.vn (hoang.nguyen@webvang.vn)
 * @copyright 2014 J&A.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @createdate 08/10/2014 09:47
 */
 
if (!defined('NV_IS_FILE_ADMIN'))
    die('Stop!!!');
$xtpl = new XTemplate('main.tpl', NV_ROOTDIR . '/themes/' . $global_config['module_theme'] . '/modules/' . $module_file);
$xtpl->assign('LANG', $lang_module);
$xtpl->assign('GLANG', $lang_global);
 
$xtpl->parse('main');
$contents = $xtpl->text('main');
 
include (NV_ROOTDIR . "/includes/header.php");
echo nv_admin_theme($contents);
include (NV_ROOTDIR . "/includes/footer.php");

Trong file func sử dụng file main.tpl, để sử dụng template này thì trong thư mục themes/' . $global_config['module_theme'] . '/modules/' . $module_file tạo file main.tpl

main.tpl
<!-- BEGIN: main -->
 
<!-- END: main -->

Giao diện đối với khu vực ngoài site

Để tách html và php ra thì cần khai báo file html (*.tpl) trong thư mục themes/' . $global_config['module_theme'] . '/modules/' . $module_file

vd: func func/main.php

main.php
<?php
 
/**
 * @Project Module Nukeviet 4.x
 * @Author Webvang.vn (hoang.nguyen@webvang.vn)
 * @copyright 2014 J&A.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @createdate 08/10/2014 09:47
 */
 
if (!defined('NV_IS_MOD_QLHS'))
    die('Stop!!!');
 
$contents = nv_page_main_list($array_data1, $array_data2, . . . );
 
include (NV_ROOTDIR . "/includes/header.php");
echo nv_admin_theme($contents);
include (NV_ROOTDIR . "/includes/footer.php");
theme.php
<?php
 
/**
 * @Project Module Nukeviet 4.x
 * @Author Webvang.vn (hoang.nguyen@webvang.vn)
 * @copyright 2014 J&A.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @createdate 08/10/2014 09:47
 */
 
if (!defined('NV_IS_MOD_QLHS'))
    die('Stop!!!');
 
function nv_page_main_list($array_data1, $array_data2, . . . . )
{
    global $module_file, $lang_module, $lang_global, $module_info, $meta_property, $my_head, $client_info, $page_config, $module_name;
 
    $template = (file_exists(NV_ROOTDIR . '/themes/' . $module_info['template'] . '/modules/' . $module_file . '/main.tpl')) ? $module_info['template'] : 'default';
 
    $xtpl = new XTemplate('main.tpl', NV_ROOTDIR . '/themes/' . $template . '/modules/' . $module_file);
    $xtpl->assign('LANG', $lang_module);
    $xtpl->assign('GLANG', $lang_global);
 
    $xtpl->parse('main');
    return $xtpl->text('main');
}

Trong file func sử dụng file main.tpl, để sử dụng template này thì trong thư mục themes/' . $template . '/modules/' . $module_file tạo file main.tpl

main.tpl
<!-- BEGIN: main -->
 
<!-- END: main -->

Nếu module có chức năng RSS

Xem Hướng dẫn chức năng RSS của module

Nếu module có chức năng tìm kiếm

$result_array[] = array( //
    'link' => $link, //
    'title' => BoldKeywordInStr($tilte, $key, $logic), //
    'content' => BoldKeywordInStr($content, $key, $logic) //
);

Là các kết quả tìm kiếm.

Cần chú ý biến $module_info trong file này không được sử dụng, thay vào đó cần dùng biến $m_values.

Để hiển thị thông tin về module

Để hiển thị thông tin về module ngay tại khu vực quản trị website, thêm file siteinfo.php vào thư mục gốc của module. Để lấy ngôn ngữ admin của module:

$lang_siteinfo = nv_get_lang_module($mod);

Cần đảm bảo file này xuất ra biến $siteinfo. Ví dụ:

$siteinfo[] = array('key' => $lang_siteinfo['siteinfo_expired'], 'value' => $number);

Xuất thông tin cho module menu

Xem Hướng dẫn xuất thông tin cho module menu

Module hỗ trợ notification

Xem Hướng dẫn notification cho module

Module hỗ trợ sitemap

Xem Hướng dẫn tạo sitemap cho module

Có kết nối với chức năng comment của hệ thống

Xem Hướng dẫn lập trình chức năng comment của hệ thống cho module

Các chức năng cần có, cách lập trình và kiểm tra của 1 function hiển thị trong module

Xem Các chức năng cần có, cách lập trình và kiểm tra của 1 function hiển thị trong module

Lấy giá trị của biến khi submit form

Gán giá trị php vào template

Trường hợp biến php là 1 giá trị cụ thể.

$lang_global = 'hoang.nguyen@webvang.vn';
$xtpl->assign('GLANG', $lang_global);

vd : này là gán biến $lang_global của php vào template (*.tpl), trong file tpl muốn in giá trị biến $lang_global ta chỉ cần gọi {GLANG}

Trường hợp biến php là 1 giá trị mảng.

$lang_global = array();
$lang_global['main'] = 'trang chính';
$lang_global['submain'] = 'trang phụ';
$xtpl->assign('GLANG', $lang_global);

Ví dụ này là gán biến $lang_global của php vào template (*.tpl), trong file tpl muốn in giá trị mãng con(main) trong biến mảng $lang_global ta chỉ cần gọi {GLANG.main}

Hướng dẫn đổi tên module NukeViet 4

Hướng dẫn đổi tên module NukeViet 4, Hướng dẫn thêm chức năng trong quản trị và ngoài site cho module Xem chi tiết tại: https://www.youtube.com/watch?v=f2IScB_nEH0

Xem thêm