huong dan viet module

Upload: quang-huy-nguyen

Post on 09-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Huong Dan Viet Module

    1/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    HNG DN VIT MODULE CHO PHIN BN NUKEVIET CMS 3.0

    CC BI VIT:

    1. Gii thiu v cu trc c bn ca module2. Cch vit mt module n gin3. Thm link qun l module vo menu qun tr4. Thm 1 submenu qun l module trong admin5. Kt ni vi file ngn ng6. Kt ni vi c sd liu7. Kt ni vi template engine (Xtemplate)8. Cc ch khi lp trnh1. Cu trc c bn khi bt u vit module

    Khc vi nhng phin bn trc y ca h thng nukeviet, trong phin bn mi ny vic ng gi

    module c ti u, cc file v folder ca module u c a vo trong 1 th mc.

    Mt module c c sd liu v c phn qun trcho ngi dng c cu trc cn bn nh sau:

    funcs

    language

    main.php

    main.php

    vi.php

    index.html

    index.html

    functions.php

    version.php

    .htaccess

    index.html

    .htaccess

    .htaccess

    .htaccess

    js

    action.php

    .htaccess

    javascript.js

    vi_admin.php

    admin.functions.php

    admin

    modules

    example

    Hnh 1: Cu trc files, folders ca mt module n gin.

    2. Cch vit mt module n gin

  • 8/8/2019 Huong Dan Viet Module

    2/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    Ch : Tn module mi ch gm cc ch ci, s v du gch ngang, bt buc phi bt u

    bng mt ch ci.

    - Trong bi vit ny chng ta xy dng mt module n gin xut ra ni dung,:Hello world!!!

    - Cc bc chun b:To cc 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

  • 8/8/2019 Huong Dan Viet Module

    3/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    modules/example/functions.php

    modules/example/version.php

    Cc ch khi khau bo trong file version.php

    - modfuncs => main, l danh sch cc cc function tng ng vi tn cc file trongfolder funcs ca module.

    - virtual => 0: Module khng c o ha- virtual=>1: Module c o ha.

    modules/example/funcs/main.php

  • 8/8/2019 Huong Dan Viet Module

    4/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    if ( ! defined( 'NV_IS_MOD_EXAMPLE' ) ) die( 'Stop!!!' );$contents = "Hello world";include ( NV_ROOTDIR . "/includes/header.php" );echo nv_site_theme( $contents );include ( NV_ROOTDIR . "/includes/footer.php" );

    ?>

    Sau khi thc hin cc bc nh trn ta c c 1 module n gin, xem kt qu bn ng nhp vo

    khu vc qun tr vi quyn admin, ci t v click hot module c th xem module qua link:

    http://mydomain.com/index.php?lang=vi&nv=example Nu bn mun thm 1 func mi cho module bn to mi 1 file (new.php) trong th mc:

    modules/example/funcs/

    Sau bn cn ng nhp v khu vc qun tr kch hot cc function ny.

    http://mydomain.com/index.php?lang=vi&nv=examplehttp://mydomain.com/index.php?lang=vi&nv=examplehttp://mydomain.com/index.php?lang=vi&nv=example
  • 8/8/2019 Huong Dan Viet Module

    5/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    3. Thm link qun l module vo menu qun tr

    modules/example/admin/functions.php

    modules/example/admin/main.php

    c th qun trmodule th trong th mc admin bt buc phi cha file: main.php v trong th mc

    tng ng vi cc file functions.php, version.php bt buc phi cha file: admin.functions.php

    Ch : cc function c th hot ng c th function phi c gn vo mng $allow_func

  • 8/8/2019 Huong Dan Viet Module

    6/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    Khi danh mc cc file ca module gm:

    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. Thm 1 submenu qun l module trong admin

    Thay i li file modules/example/admin.functions.php

  • 8/8/2019 Huong Dan Viet Module

    7/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    5. Kt ni vi file ngn ng

    Sa li ni dung file modules/example/admin.functions.php

    Quy tc t tn file ngn ng: Cc file ngn ngc t trong th mc language ca module, Nu

    ngn ng cho module st tn: vi.php, en.php, ngn ng cho admin admin_vi.php, admin_en.php

    Ta to file ngn ng ting Vit cho module example:

    modules/example/language/ admin_vi.php

  • 8/8/2019 Huong Dan Viet Module

    8/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    Khi danh mc cc file ca module gm:

    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

  • 8/8/2019 Huong Dan Viet Module

    9/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    6. Kt ni vi c sdliu

    modules/example/action.php

    Sau khi to xong file action.php, bn vo phn qun l module ci li module example. Khi csd liu sc to tng.

    Sa li file modules/example/admin/add.php

  • 8/8/2019 Huong Dan Viet Module

    10/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    else{

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

    if ( $db->sql_query_insert_id( $query ) ){

    $db->sql_freeresult();Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABL

    E . "=" . $module_name . "" );die();

    }else{

    $error = $lang_module['errorsave'];}

    }}if ( $error != "" ){

    $contents .= "\n";

    $contents .= "" . $error . "\n";

    $contents .= "\n";$contents .= "\n";

    }$contents .= "\n";$contents .= "\n";$contents .= " \n";$contents .= " " . $lang_module['title'] . "\n";$contents .= " \n";$contents .= " \n";$contents .= " \n";$contents .= " \n";

    $contents .= "\n";$contents .= "\n";$contents .= " \n";$contents .= " \n";$contents .= " \n";$contents .= " \n";$contents .= " ";$contents .= " ";$contents .= " \n";$contents .= " \n";$contents .= "\n";

    $contents .= "\n";

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

    ?>

  • 8/8/2019 Huong Dan Viet Module

    11/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    Sa li file modules/example/language/vi_admin.php

  • 8/8/2019 Huong Dan Viet Module

    12/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    Sa li file modules/example/admin/main.php

  • 8/8/2019 Huong Dan Viet Module

    13/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    Sa li file modules/example/funcs/main.php

    Danh mc cc 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

  • 8/8/2019 Huong Dan Viet Module

    14/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    7. Kt ni vi template engine (Xtemplate)

    Bt u t phin bn nukeviet 3.0, h thng template c tch bit hn ra vi m php. Hin ti,h thng template ca nukeviet ang s dng Xtemplate x l cho vic tch bit m php v html.

    tm hiu thm v cch vit Xtemplate, cc bn c th c thm ti liu v ti a ch:http://sourceforge.net/projects/xtpl/files/XTemplate%20PHP5/S template admin control panel s dng Xtemplate

    themes

    admin_default

    css

    images

    js

    tpl

    example.css

    modules

    example.php

    example.tpl Hnh 2: S cu trc template admin control panel s dng Xtemplate

    s dng Xtemplate cho admin control panel ca module Example ta tin hnh cc bc nh sau:

    Sa li file modules/example/admin/main.php

    Sa li file modules/example/admin/add.php

    http://sourceforge.net/projects/xtpl/files/XTemplate%20PHP5/http://sourceforge.net/projects/xtpl/files/XTemplate%20PHP5/http://sourceforge.net/projects/xtpl/files/XTemplate%20PHP5/
  • 8/8/2019 Huong Dan Viet Module

    15/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    Sa li file modules/example/admin/edit.php

  • 8/8/2019 Huong Dan Viet Module

    16/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    $error = "";$page_title = $lang_module['example_edit'];

    $id = $nv_Request->get_int( 'id', 'get,post', 0 );if ( $id > 0 ){

    if ( $nv_Request->get_int( 'save', 'post' ) == 1 ){

    $title = filter_text_input( 'title', 'post', '', 1 );if ( empty( $title ) ){

    $error = $lang_module['err_title'];}else{

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

    $db->sql_query( $query );$db->sql_freeresult();Header( "Location: " . NV_BASE_ADMINURL . "index.php?" .

    NV_NAME_VARIABLE . "=" . $module_name . "" );}}else{

    $sql = "SELECT title FROM `" . NV_PREFIXLANG . "_" . $module_data . "` WHERE`id` = '" . $id . "'";

    $resuilt = $db->sql_query( $sql );list( $title ) = $db->sql_fetchrow( $resuilt );

    }$contents = call_user_func("edit",$id, $title, $error);}include ( NV_ROOTDIR . "/includes/header.php" );echo nv_admin_theme( $contents );include ( NV_ROOTDIR . "/includes/footer.php" );

    ?>

    Thm file themes/admin_default/modules/example.php. Lu : Tn file trong th mc module ca

    template admin phi trng tn vi tn module.

  • 8/8/2019 Huong Dan Viet Module

    17/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    global $global_config, $lang_module,$module_data, $db_config, $nv_Request,$module_name, $op;

    $xtpl = new XTemplate ( "example.tpl", NV_ROOTDIR . "/themes/" .$global_config ['module_theme'] . "/modules" );

    $xtpl->assign('LANG',$lang_module);$xtpl->assign('NV_BASE_ADMINURL',NV_BASE_ADMINURL);$xtpl->assign('NV_NAME_VARIABLE',NV_NAME_VARIABLE);$xtpl->assign('MODULE_NAME', $module_name);$xtpl->assign('TITLE', $title);$xtpl->assign('ID', $id);$xtpl->assign('OP', $op);if(!empty($error)){

    $xtpl->assign('ERROR',$error);$xtpl->parse('edit.error');

    }$xtpl->assign('NV_OP_VARIABLE',NV_OP_VARIABLE);$xtpl->parse('edit');return $xtpl->text('edit');

    }

    function main($content){global $global_config, $lang_module,$module_data, $db_config, $nv_Request,

    $module_name, $op;$xtpl = new XTemplate ( "example.tpl", NV_ROOTDIR . "/themes/" .

    $global_config ['module_theme'] . "/modules" );$xtpl->assign('LANG',$lang_module);if(!empty($content)){

    foreach($content as $i => $row){

    $xtpl->assign('CLASS',($i % 2) ? '' : ' class="second"');$xtpl->assign('EDIT',"" .$lang_module['example_edit'] . "");

    $xtpl->assign('DEL',"" .$lang_module['example_del'] . "");

    $xtpl->assign('CONTENT', $row);$xtpl->parse('main.loop');

    }}$xtpl->parse('main');return $xtpl->text('main');

    }?>

    Thm file themes/admin_default/modules/example.tpl

    {LANG.title}

  • 8/8/2019 Huong Dan Viet Module

    18/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    {ERROR}

    {LANG.title}

    {ERROR}

    {LANG.id}{LANG.title}{LANG.func}

    {CONTENT.id}{CONTENT.title}

    {EDIT} - {DEL}

    Danh sch cc file:

    modules/example/admin/index.html

  • 8/8/2019 Huong Dan Viet Module

    19/24

  • 8/8/2019 Huong Dan Viet Module

    20/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    }$contents = call_user_func( "main_theme", $content );include ( NV_ROOTDIR . "/includes/header.php" );echo nv_site_theme( $contents );include ( NV_ROOTDIR . "/includes/footer.php" );?>

    Thm mi file modules/example/theme.php vi ni dung nh sau:

    Ch :Tn ca file trong th mc modules ca template phi trng vi tn ca module.

  • 8/8/2019 Huong Dan Viet Module

    21/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    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

  • 8/8/2019 Huong Dan Viet Module

    22/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    Cc ch khi lp trnh1) Encoding

    Encoding mc nh tt c cc file l: ansi, cc file lang nh ting vit, php, nga .. Encoding

    cn chuyn sang utf-82) Trigger_error

    thay v dng die("...") bo li v dng chng trnh; cn dng dng trigger_error("...", 256);

    ghi li li v thng bo li.

    3) Ch thng bo li

    thay i ch thng bo li ca php cn sa file includes /constants.php//define( "NV_DISPLAY_ERRORS_LIST", E_ALL | E_STRICT ); //Danh sach cac loi sehien thi

    define( "NV_DISPLAY_ERRORS_LIST", E_ALL );//define( "NV_DISPLAY_ERRORS_LIST", 0); //tat thong bao loidefine( "NV_LOG_ERRORS_LIST", E_ALL | E_STRICT ); //Danh sach cac loi se ghi l

    ogdefine( "NV_SEND_ERRORS_LIST", E_USER_ERROR ); //Danh sach cac loi se gui denemail

    VD: tt thng bo li ta cn sa on trn thnh

    //define( "NV_DISPLAY_ERRORS_LIST", E_ALL | E_STRICT ); //Danh sach cac loi sehien thi

    //define( "NV_DISPLAY_ERRORS_LIST", E_ALL );define( "NV_DISPLAY_ERRORS_LIST", 0); //tat thong bao loidefine( "NV_LOG_ERRORS_LIST", E_ALL | E_STRICT ); //Danh sach cac loi se ghi logdefine( "NV_SEND_ERRORS_LIST", E_USER_ERROR ); //Danh sach cac loi se gui denemail

    Nhung cai trang nao ko cho phep truy cap truc tiep neu khong co referer

    can phai co dong nay

    if ( ! defined( 'NV_IS_AJAX' ) ) die( 'Wrong URL' );

  • 8/8/2019 Huong Dan Viet Module

    23/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    4) Ly gi tr ca bin khi submit form

    class request cn c th ly theo cc phng thc: get_bool, get_string, get_int, get_float, get_array,

    Trong phng thc get_string nu ly qua phng thc get, gi tr ca bin c lc m html

    Ly bin dng s nguyn

    Ly bin dng s thc

    S dng hm: filter_text_input

  • 8/8/2019 Huong Dan Viet Module

    24/24

    VIETNAM OPEN SOURCE DEVELOPMENT JOIN STOCK COMPANYAddress: 67b Khuong Ha, Khuong inh, Thanh Xuan, Ha Noi

    Tel: (04) 8587 2007Fax: (04) 3550 0914Email: [email protected]: www.vinades.vn

    html c bit// Gi tr ca bin ly 255 k tu tin, nu gi tr nhiu hn 255 k t th cc k t sau 255 s b ct i

    $default = "default";$preg_replace = array( 'pattern' => "/[^a-zA-Z0-9]/", 'replacement' => "_");$value = filter_text_input( 'input_name', 'request', $default, 0, 100, $preg_replace );

    // Ly gi tr ca input_name qua phng thc request, nu khng c gi tr bng default// Gi tr ca bin ly 100 k tu tin, nu gi tr nhiu hn 100 k t th cc k t sau 100 s b ct i// Gi tr ca bin s thay th cc k t khc a-zA-Z0-9 bng k t gch di

    ?>

    Sdng hm: filter_text_textarea

    khi 1 form c textarea, ch dng phng thc post, chkhng dng phng thc get, nukhng s b mt d liu

    s dng filter_text_textarea cho cc editor

    s dng filter_text_textarea cho cc textarea khng s dng editor