ApacheとかXOOPSはインストールされているものとする。
ここ でスケルトンを作成できるので作ってmodulesディレクトリの下におく。ここではモジュール名はbunkoにした。そしてこのモジュールを管理画面からインストールする。成功したらトップページのメニューにBUNKOが追加されるので、クリックして実行できた。
一般ユーザ(非登録ユーザ)で実行できないので、以下のようにしてアクセス可能にする。
あとは作っていくわけだがこれもなかなか難しい。デフォルトのindex.phpは以下のようになっている。
<?php
include 'header.php' ;
// this page uses smarty template
// this must be set before including main header.php
$xoopsOption [ 'template_main' ] = 'bunko_index.html' ;
include XOOPS_ROOT_PATH . '/header.php' ;
$xoopsTpl -> assign ( 'lang_title' , _XD_TITLE ) ;
include XOOPS_ROOT_PATH . '/footer.php' ;
?>
bunko_index.htmlはtemplatesディレクトリにあるファイルを指定している。これがテンプレートになっていて、このファイルの中で使われる変数の定義をしているのがassignの行だろう。_XD_TITLEはlanguageで定義されている。つまりbunko_index.htmlがMVCのビューに相当して、index.phpがモデルとコントローラになるのか?
このスケルトンにはデータベース関係が含まれていないので、少し書いてみたい。mysqlディレクトリにあるmysql.sqlはこのモジュールのインストール時に実行され、必要なデータベースが作られると思われる。まずmysql.sqlを以下のように編集する。
CREATE TABLE bunkodata (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
title VARCHAR ( 511 ) NOT NULL
) ENGINE = InnoDB
次にxoops_version.phpの以下の行のコメントをはずしてからモジュールの再インストールを行ってみる。
$modversion [ 'sqlfile' ] [ 'mysql' ] = "sql/mysql.sql" ;
fc6515_bunkodata ができた。
このテーブルにアクセスしてみる。ここではあらかじめデータを入れておき読み込みのみを考える。入ってるデータは以下。
まずbunko_index.htmlを以下のようにする。
<h4 style = "text-align:left;" > <{$lang_title}> </h4>
<table>
<tr> <td> id </td> <td> title </td> </tr>
<{foreach item= data from= $ zenbu} >
<tr> <td> <{$data.id}> </td> <td> <{$data.title}> </td> </tr>
<{/foreach}>
</table>
<{で始まってるのはsmartyの文法なのか?まあいいや。
次にindex.phpを以下のようにする。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
include 'header.php' ;
// this page uses smarty template
// this must be set before including main header.php
$xoopsOption [ 'template_main' ] = 'bunko_index.html' ;
include XOOPS_ROOT_PATH . '/header.php' ;
$xoopsTpl -> assign ( 'lang_title' , _XD_TITLE ) ;
$sql = "SELECT id, title FROM " . $xoopsDB -> prefix ( "bunkodata" ) ;
$result = $xoopsDB -> query ( $sql ) ;
while ( $row = $xoopsDB -> fetchArray ( $result ) )
{
$zenbu [ ] = $row ;
}
$xoopsTpl -> assign ( 'zenbu' , $zenbu ) ;
include XOOPS_ROOT_PATH . '/footer.php' ;
?>
templateを編集した場合は、モジュールのアップデートをしなければならないのでアップデートしてから以下のようにできた。
最後にはユーザによる処理分けについて。
これは$xoopsUserというのを使うらしい。これが定義されていなければ未登録ユーザ。定義されていればいろいろ情報を持ってこれる。
これを使って以下のようにできた。
bunko_index.html
<h4 style = "text-align:left;" > <{$lang_title}> </h4>
Hello <{$username}> !!
<table>
<tr> <td> id </td> <td> title </td> </tr>
<{foreach item= data from= $ zenbu} >
<tr> <td> <{$data.id}> </td> <td> <{$data.title}> </td> </tr>
<{/foreach}>
</table>
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
include 'header.php' ;
// this page uses smarty template
// this must be set before including main header.php
$xoopsOption [ 'template_main' ] = 'bunko_index.html' ;
include XOOPS_ROOT_PATH . '/header.php' ;
$xoopsTpl -> assign ( 'lang_title' , _XD_TITLE ) ;
if ( $xoopsUser )
$xoopsTpl -> assign ( 'username' , $xoopsUser -> uname ( ) ) ;
else
$xoopsTpl -> assign ( 'username' , "GUEST" ) ;
$sql = "SELECT id, title FROM " . $xoopsDB -> prefix ( "bunkodata" ) ;
$result = $xoopsDB -> query ( $sql ) ;
while ( $row = $xoopsDB -> fetchArray ( $result ) )
{
$zenbu [ ] = $row ;
}
$xoopsTpl -> assign ( 'zenbu' , $zenbu ) ;
include XOOPS_ROOT_PATH . '/footer.php' ;
?>
難しかった。
★ここにあるソースのライセンスはGPLを継承。