注文の多いXOOPS屋 TOP現在のブログ旧スタッフblog アーカイブ
このページは 2007〜2008 年に公開していた「株式会社RYUS スタッフblog」のアーカイブです。内容は当時のものです。

cubson 用モック表示アクション

2007/06/01 15:24   Ryuji   カテゴリ: cubson

ご依頼を受けてモジュールを開発するときに、先に画面だけのモックをつくってお客様にに確認していただくことがあります。
この時に作成する HTML がテンプレートのベースになります。

モジュール開発はもっぱら cubson を利用しているのですが、このモックを表示するためだけのアクションを作成してみました。

下記は hoge モジュールでの例です。モック用テンプレートを XOOPS_ROOT_PATH/SampleHtml/ 下に置いています。

class Hoge_MockAction extends Hoge_AbstractAction 
{
var $file;
var $action;
var $template_path;
function prepare()
{
$this->template_path = XOOPS_ROOT_PATH .'/SampleHtml';
}
function getDefaultView()
{
$this->action = xoops_getrequest('action');
if($this->action === null){
$this->action = "event_list";
}

$file = $this->template_path .'/'.$this->action.'.html';
if(file_exists($file)){
$this->file = $this->action.'.html';
return HOGE_FRAME_VIEW_SUCCESS;
}else{
return HOGE_FRAME_VIEW_ERROR;
}
}
function executeViewSuccess(&$render)
{
$tpl = new XoopsTpl();
$tpl->xoops_setTemplateDir($this->template_path);
$tpl->force_compile = TRUE;
$html = $tpl->fetch($this->file);

$render->setTemplateName("legacy_dummy.html");
$render->setAttribute('dummy_content', $html);
}
function executeViewError(&$render)
{
$render->setTemplateName("legacy_dummy.html");
$render->setAttribute('dummy_content', $this->action." not found.");
}
}/PRE

さらに、class/Module.class.php の execute メソッドを一部改造します。

		if (!file_exists($fileName)) {
//$this->doActionNotFoundError();
//die();
}

上記の用になっている箇所を下記のように変更します。
		if (!file_exists($fileName)) {
$this->mActionName = "Mock";
$fileName = "MockAction";
if ($this->mAdminFlag) {
$className = "Hoge_Admin_" . ucfirst($this->mActionName) . "Action";
$fileName = XOOPS_MODULE_PATH . "/hoge/admin/actions/${fileName}.class.php";
}
else {
$className = "Hoge_" . ucfirst($this->mActionName) . "Action";
$fileName = XOOPS_MODULE_PATH . "/hoge/actions/${fileName}.class.php";
}
//$this->doActionNotFoundError();
//die();
}

これで、action=foo としたときに、FooAction.class.php が存在していれば、FooAction を実行し、
なければ、MockAction を実行して、foo.html を表示するようになります。

モック HTML をつくるときに、リンク先を index.php?action=foo とするなどアクション名を意識してモックをつくっておけば、アクションが実装された部分はきちんとアクションが実行され、未実装部分はモックで表示されるので画面遷移のイメージを保ちながら開発できるかと思います。