Xây dựng module sử dụng Support Suite

Các vấn đề đề cập:

  1. Tạo một module mới
  2. Xử lý quá trình cài đặt
  3. Tạo một tab mới trong staff panel
  4. Tạo widget ở trang Client

Thiết lập module

1. Tạo một thư mục tên helloworld dưới thư mục ./modules
2. Tạo một tập tin setup.php dưới thư mục vừa tạo
3. Xây dựng kịch bản xử lý quá trình cài đặt module:
- Tạo truy vấn, đăng ký module với hệ thống
- Tạo truy vấn xây dựng bảng `helloworld` trong CSDL

<?php
  
function _helloworld_install($action$page '') {
    global 
$_SWIFT$__LANG;

    if (
$action == "getpages") {
      return 
"1";
    }
    else if (
$action == "buildpage") {
      
$_SWIFT["query"]["create"]["helloworld"]="
        CREATE TABLE "
.TABLE_PREFIX."helloworld (
          helloworld_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
          helloworld_text VARCHAR(255) NOT NULL DEFAULT '',
          PRIMARY KEY (helloworld_id)
      );"
;
   
      
//Tell Esupport about the module.
      
$_SWIFT["query"]["insert"]["settings"][0]= "
        INSERT INTO `"
.TABLE_PREFIX."settings`
          (`section`, `vkey`, `data`)
          VALUES('registeredmodules', '"
.MODULE_HELLOWORLD."', '1')";
    }
  }
?>

4. Xây dựng kịch bản xử lý quá trình gỡ bỏ module:

<?php
  
function _helloworld_uninstall() {
    global 
$_SWIFT;
   
    
$_SWIFT["query"]["drop"]["helloworld"]="DROP TABLE `".TABLE_PREFIX."helloworld;";
   
    
//Tell Esupport We are removing the module.
    
$_SWIFT["query"]["delete"]["settings"][0]= "
      DELETE FROM `"
.TABLE_PREFIX."settings` WHERE `section` = 'registeredmodules' AND `vkey` = '".MODULE_HELLOWORLD."';";
  }
?>

5. Tìm trong tập tin config/config.php các dòng:

<?php
  define
("MODULE_PURCHASE""purchase");
  
define("MODULE_SERVERS""servers");
?>

6. Thêm vào ngay sau 2 dòng trên đoạn mã:

<?php
  define
("MODULE_HELLOWORLD","helloworld");
?>

7. Tìm trong tập tin config/config.php các dòng:

<?php
  $_MODULES
[MODULE_PURCHASE] =
  
dirname(__FILE__)."/../modules/purchase/purchase.php";
  
$_MODULES[MODULE_SERVERS] =
  
dirname(__FILE__)."/../modules/servers/purchase.php";
?>

8. Thêm vào ngay sau 2 dòng trên đoạn mã:

<?php
  $_MODULES
[MODULE_MLS] =
  
dirname(__FILE__)."/../modules/helloworld/helloworld.php";
?>

Đến bước này thì các module đã được đăng ký với hệ thống. Tuy nhiên, để module làm được cái gì đó, chúng ta phải viết hàm xử lý các sự kiện.

Xử lý sự kiện

9. Tạo tập tin modules/helloworld/helloworld.php với nội dung như sau:

<?php
  
if (!defined("INSWIFT")) {
    
trigger_error("Unable to process $PHP_SELF"E_USER_ERROR);
  }
  
  class 
helloworld {
/*
 * đăng ký các thao tác với hệ thống
 */
    
function helloworld() {
      global 
$_SWIFT$events;
      
$events->registerEventModuleSWIFT(MODULE_HELLOWORLD$this);
      
// Staff panel
      
$events->registerEvent(EVENT_STAFFMODULE_HELLOWORLD"hello1");
      
$events->registerEvent(EVENT_STAFFMODULE_HELLOWORLD"hello2");
      
$eventaction $events->eventdata["eventaction"];
    }

    function 
_helloworld() {
    }
/*
 * các thao tác xử lý
 */
    
function _eventcallback($eventtype$eventaction) {
      
// các biến toàn cục của hệ thống
      
global
          
$_SWIFT$interface$template,
          
$departments$staffauth$module$grid$xml,
          
$staff$settings$dbCore$events,
          
$session$loginshare$cookie$errormessage,
          
$infomessage$permissions$mimelist;
  
      
// bắt sự kiện 1
      
if ($eventtype == EVENT_STAFF &amp;&amp; ($eventaction == "hello1")) {
        require_once (
"./modules/helloworld/hello1.php");
        return 
true;
  
      
// bắt sự kiện 2
      
} else if ($eventtype == EVENT_STAFF && ($eventaction == "hello2")) {
        require_once (
"./modules/helloworld/hello2.php");
        return 
true;
      }

      
// không có sự kiện nào thích hợp
      
return false;
    }
  }
?>

Đến đây, module của chúng ta đã biết cách xử lý 2 sự kiện. Bây giờ, chúng ta sẽ đi xây dựng kịch bản chi tiết cho 2 sự kiện đó.

10. Tạo tập tin modules/helloworld/hello1.php với nội dung

<?php
  $template
->loadLanguageSection("helloworld");
  
$interface->staffHeader("helloworld&amp;gt; hello1"0);
  
$interface->staffNavBar('&lt;a href="index.php?_m=helloworld&amp;_a=hello1"
  title="hello1">hello1&lt;/a>'
""0);
  
// printInfoBox($infomessage);
  // printErrorBox($errormessage);
  
echo "Helloworld1!";
  
$template->assign("backurl""index.php?_m=helloworld&amp;_a=hello1");
  
$interface->staffFooter();
?>

Đến bước này, khi chúng ta thực hiện yêu cầu ?_m=helloworld&amp;_a=hello1, module sẽ gọi đoạn mã trên để thực hiện kịch bản mà chúng ta đã xây dựng.
Lặp lại từ đầu bước 10 để xây dựng kịch bản cho sự kiện hello2.

Tạo tab

11. Mở tập tin locale/en-us/en-us.php

12. Tìm dòng

<?php
  $_SWIFT
["staffmenu"] = array (
?>

13. Thêm vào cuối mảng này phần tử:

<?php
  10 
=> array ('Rights'90MODULE_HELLOWORLD3'hw_entab'),
?>

Đây là tab mới trong staff panel của chúng ta.
14. Tìm tiếp theo trong tập tin này dòng sau:

<?php
  $_SWIFT
["stafflinks"] = array (
?>

15. Thêm vào cuối mảng này các phần tử:

<?php
  10 
=> array (
    
=> array ('Hello1'"index.php?_m=helloworld&amp;_a=hello1"falsetrue),
    
=> array ('Hello21'"index.php?_m=helloworld&amp;_a=hello2"truetrue),
    
=> array ('Hello2'"index.php?_m=helloworld&amp;_a=hello2"truefalse),
  ),
?>

Đây là các sub-tab items trong staff panel của chúng ta. Menu của chúng ta có mã số là 10, cho nên chúng ta phải thay đổi cấp trên của hello1 và hello2 trong đoạn mã

16. Tìm trong tập tin modules/helloworld/hello1.php đoạn mã sau:

<?php
  $interface
->staffHeader("helloworld&amp;gt; hello1"0);
  
$interface->staffNavBar('&lt;a href="index.php?_m=helloworld&amp;_a=hello1"
  title="hello1">hello1&lt;/a>'
""0);
?>

Chú ý chuỗi: , 0);, thay thành , 10);. Lúc này, chúng ta cần xóa bộ đệm của hệ thống: thực hiện yêu cầu đến:

http://OurWebsite.com/staff/index.php?_ca=rebuildcache

Thoát ra và sau đó đăng nhập lại vào Staff Panel. (Nếu như tab của panel không có thay đổi gì, thử xóa bộ đệm của trình duyệt web).
Để tạo tab mới trong Admin panel, thực hiện tương tự như từ bước 11, nhưng SWIFT_AREA lúc này sẽ là SWIFT_ADMIN

<?php
  
if (SWIFT_AREA == SWIFT_ADMIN)
?>

Để tùy biến một số thiết lập cơ bản với Admin Tab, chúng ta cần xem qua tập tin modules/core/admin_settings.php (cần thêm một số dòng dữ liệu vào CSDL).

Tạo một widget

17. Mở tập tin includes/widgets/widgets.php, ở đây, chúng ta tìm thấy tất cả các widgets và các quyền hạn ràng buộc của nó. Tiếp theo, chúng ta sẽ thêm vào một widget rất căn bản.

18. Tìm đoạn mã:

<?php
  
/**
  * ###############################################
  * !! DO NOT EDIT ANYTHING BELOW THIS POINT !!
  * ###############################################
  */
?>

19. Thêm vào phía trước nó đoạn mã sau:

<?php
  
// ======= HELLOWORLD =======
  
if ($module->isRegistered(MODULE_HELLOWORLD)) {
    
$_widgets[$index]["icon"] = $_SWIFT["themepath"]."helloworld.gif";
    
$_widgets[$index]["title"] = "Helloworld";
    
$_widgets[$index]["description"] = "Hello world!";
    
$_widgets[$index]["link"] = "index.php?_m=helloworld&amp;_a=view";
    
$index++;
  }
?>

20. Tùy biến: đừng quên đặt vào thư mục theme tập tin helloworld.gif của bạn. Hay là tập tin nào khác cũng được, tùy theo khai báo widget của chúng ta:

<?php
  $_widgets
[$index]["icon"] = $_SWIFT["themepath"]."helloworld.gif";
?>

21. Chúng ta có thể không gắn cứng tiêu đề và mô tả widget nhờ chức năng đa ngôn ngữ của hệ thống:

<?php
  $_widgets
[$index]["title"] = $_SWIFT["language"]["helloworld"];
  
$_widgets[$index]["description"] = $_SWIFT["language"]["desc_helloworld"];
?>

Bây giờ, chúng ta phải xử lý sự kiện mà chúng ta đã gán trong liên kết ("index.php?_m=helloworld&_a=view")

22. Mở tập tin modules/helloword/helloworld.php
23. Trong hàm

<?php
  
function helloworld() {
?>

Thêm vào sau các sự kiện trong Stafff panel đoạn mã sau:

<?php
  
// Support Center
  
$events->registerEvent(EVENT_CLIENTMODULE_HELLOWORLD"view");
?>

24. Trong hàm

<?php
  
function _eventcallback($eventtype$eventaction)
?>

25. Thêm vào cuối các mệnh đề so sánh, mệnh đề của chúng ta:

<?php
  
else if ($eventtype == EVENT_CLIENT &amp;&amp; ($eventaction == "view")) {
    require_once (
"./modules/helloworld/view.php");
    return 
true;
  }
?>

26. Tạo tập tin modules/helloworld/view.php với nội dung sau:

<?php
  
if (!defined("INSWIFT")) {
    
trigger_error("Unable to process $PHP_SELF"E_USER_ERROR);
  }

  echo 
$template->displayTemplate("header");
  echo 
"Hello world";
  
$template->assign("backurl""index.php");
  echo 
$template->displayTemplate("footer");
?>

Để có thêm các thông tin về các chức năng nâng cao về xử lý giao diện, xem qua các module đã xây dựng sẵn.

Comments

Xao qua!

"Hiện đang có 12 thành viên và 503 khách trực tuyến." Xạo qua!

hum, kha hay day

He nho cac anh cac chi, xin cac anh chi chi giao them, em moi 13 tuoi thui
Em da co bang A, B, C, TOEFL, TOEIC. (Dang ne day chu!)
Em mun trau doi kha nang noi tieng Anh cua minh
Rat mong cac anh chi chap nhan dua em nho xiu nay

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <sup> <br> <p> <h3>
  • Lines and paragraphs break automatically.
  • Link to content with [[some text]], where "some text" is the title of existing content or the title of a new piece of content to create. You can also link text to a different title by using [[link to this title|show this text]]. Link to outside URLs with [[http://www.example.com|some text]], or even [[http://www.example.com]].
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is used to make sure you are a human visitor and to prevent spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.