第8回 HTML_QuickForm_Controller大好き!
その1「ページ間を飛んでみる」
ご意見・ご感想
コラム
  まず、3ページ作って、ページ間を移動してみます。こんな感じの動きです。
  準備作業として、次のようなファイルcommonTips.phpを作ってください。これは他のサンプルを作るときにも使用するので別ファイルにしておきます。tipsDisplayとわざわざしているのは、ここでデバッグ用出力などすれば便利だからです。
<?php
////////////////////////////////////////////////////////////////////////////////
//  commonTips.php 共通処理
//  2005.10.10 作成
////////////////////////////////////////////////////////////////////////////////
require_once "Smarty.class.php";

require_once 
"HTML/QuickForm.php";

require_once 
"HTML/QuickForm/Renderer/ArraySmarty.php";
require_once 
"HTML/QuickForm/Rule.php";

require_once 
"HTML/QuickForm/Controller.php";
require_once 
"HTML/QuickForm/Action/Next.php";
require_once 
"HTML/QuickForm/Action/Back.php";
require_once 
"HTML/QuickForm/Action/Jump.php";
require_once 
"HTML/QuickForm/Action/Submit.php";
require_once 
"HTML/QuickForm/Action/Display.php";

// Smartyの処理
class tipsSmarty extends Smarty {
  
// コンストラクタ 
  
function tipsSmarty() {
    
$this->Smarty();
    
$this->template_dir "./templates";
    
$this->compile_dir "./templates_c";
  }

  
// 表示処理
  // $strPageName 表示するページ名
  
function tipsDisplay(&$strPageName) {
    
$this->display($strPageName '.tpl.html');
  }
}
?>
  次にsimple.phpを作ってください。simple.phpのFirstPage,SecondPage,LastPageは同じようなコードです。これを何度も同じコードを書くのではなくもっとスマートに書く方法もあるのですが、それはまたおいおい説明ということで、今回は最初なのでベタで書いちゃってください。
<?php
////////////////////////////////////////////////////////////////////////////////
//  simple.php 
//  2005.10.10 作成
////////////////////////////////////////////////////////////////////////////////

require_once './commonTips.php';

// 1ページ目
class FirstPage extends HTML_QuickForm_Page {
    function 
buildForm() {
        
$this->_formBuilt true;
        
$this->addElement('static''lblPageNo''',
                          
'これは1ページです。');
        
$this->addElement('submit',
                          
$this->getButtonName('AShowFirst'),
                          
'1ページを表示');
        
$this->addElement('submit',
                          
$this->getButtonName('AShowSecond'),
                          
'2ページを表示');
        
$this->addElement('submit',
                          
$this->getButtonName('AShowLast'),
                          
'3ページを表示');
        
$this->setDefaultAction('next');
    }


// 2ページ目
class SecondPage extends HTML_QuickForm_Page {
    function 
buildForm() {
        
$this->_formBuilt true;
        
$this->addElement('static''lblPageNo''',
                          
'これは2ページです。');
        
$this->addElement('submit',
                          
$this->getButtonName('AShowFirst'),
                          
'1ページを表示');
        
$this->addElement('submit',
                          
$this->getButtonName('AShowSecond'),
                          
'2ページを表示');
        
$this->addElement('submit',
                          
$this->getButtonName('AShowLast'),
                          
'3ページを表示');
        
$this->setDefaultAction('next');
    }
}

// 3ページ目
class LastPage extends HTML_QuickForm_Page {
    function 
buildForm() {
        
$this->_formBuilt true;
        
$this->addElement('static''lblPageNo''',
                          
'これは3ページです。');
        
$this->addElement('submit',
                          
$this->getButtonName('AShowFirst'),
                          
'1ページを表示');
        
$this->addElement('submit',
                          
$this->getButtonName('AShowSecond'),
                          
'2ページを表示');
        
$this->addElement('submit',
                          
$this->getButtonName('AShowLast'),
                          
'3ページを表示');
        
$this->setDefaultAction('next');
    }
}

// 1ページを表示
class ActionShowFirst extends HTML_QuickForm_Action {
    function 
perform(&$page$actionName)
    {
        
$page->isFormBuilt() or $page->buildForm();
        
$next =& $page->controller->getPage('PFirst');
        
$next->handle('jump');
    }
}

// 2ページを表示
class ActionShowSecond extends HTML_QuickForm_Action {
    function 
perform(&$page$actionName)
    {
        
$page->isFormBuilt() or $page->buildForm();
        
$next =& $page->controller->getPage('PSecond');
        
$next->handle('jump');
    }
}

// 3ページを表示
class ActionShowLast extends HTML_QuickForm_Action {
    function 
perform(&$page$actionName)
    {
        
$page->isFormBuilt() or $page->buildForm();
        
$next =& $page->controller->getPage('PLast');
        
$next->handle('jump');
    }
}

class 
ActionDisplay extends HTML_QuickForm_Action_Display {
  function 
_renderForm(&$page) {

    
$smartyObj = new tipsSmarty();

    
$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($smartyObj);
    
$page->accept($renderer);
    
$smartyObj->assign('page'$renderer->toArray());
    
$page_action $page->controller->getActionName();
    
$smartyObj->tipsDisplay($page_action[0]);
  }
}

session_start();

$controller =& new HTML_QuickForm_Controller('form'false);

//  必要なページをすべて追加
$controller->addPage(new FirstPage('PFirst'));
$controller->addPage(new SecondPage('PSecond'));
$controller->addPage(new LastPage('PLast'));

//  ハンドラ設定
$controller->addAction('next', new HTML_QuickForm_Action_Next());
$controller->addAction('back', new HTML_QuickForm_Action_Back());
$controller->addAction('jump', new HTML_QuickForm_Action_Jump());
$controller->addAction('AShowFirst', new ActionShowFirst());
$controller->addAction('AShowSecond', new ActionShowSecond());
$controller->addAction('AShowLast', new ActionShowLast());
$controller->addAction('display', new ActionDisplay());

//  実行
$controller->run();

?>
  最後にcommonTips.phpとsimple.phpを作ったディレクトリにtemplatesディレクトリとtemplates_cディレクトリを作ってください。そしてtemplatesの下に3ページ分のテンプレートを作ります。似たようなものを3つ作ればよいので最初のページ(PFirst.tpl.html)だけ載せておきます。
  ファイル名は[ページ名].tpl.htmlとします。ページ名はsimple.phpの下の方「必要なページを全て追加」部分でaddPageした時に付けた名前'PFirst','PSecond','PLast'です。
テンプレートの「ボタンたち」の部分で定義しているように、ボタンの名前は'_qf_[ページ名]_[アクション名]'となります。アクション名はaddPageの次に「ハンドラ設定」でaddActionした時に付けた名前'AShowFirst','AShowSecond','AShowLast'です。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>HTML_QuickFormController-Tips</title>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">
</head>
<body bgcolor="#99ccff">
    <form {$page.attributes}>
      </table>
      <table width="100%" border="0">
        <tr>
          <td align="center">
            {$page.lblPageNo.html}<br><br>
          </td>  
        </tr>
        <tr>
          <td align="center">
            <!-- ボタンたち -->
            {$page._qf_PFirst_AShowFirst.html}<br><br>
            {$page._qf_PFirst_AShowSecond.html}<br><br>
            {$page._qf_PFirst_AShowLast.html}
          </td>
        </tr>
      </table>
    </form>
</body>
</html>
  これで完成です。simple.phpを動かして、ページ間を飛んでみてください。なんだか楽しくないですか??
  しかし、説明書くのって大変ですね。(ってほとんど説明できてないです。とにかく作って動かしてみればわかるかと…) もはや力尽きそうです。いつもいろんなサイトを参考にさせてもらってるのですが、本当にありがたいことなんだと改めて感謝しました。

第8回 項目一覧 サンプル
HTML_QuickForm_Controller大好き!  
その1「ページ間を飛んでみる」 ページ間を飛ぶ
その2「ラジオボタンをリストに表示する」 ラジオボタンのリスト
その3「チェックボックスもリストに表示する」 チェックボックスのリスト
その4「リスト(チェックボックスを使用)に追加・削除する」 リストの追加削除1
その5「ボタンをリストに表示する(JavaScriptを使用)」 リストの追加削除2
その6「無理矢理リンクでの遷移にも対応(JavaScriptを使用)」
ついでに「似たようなページを手抜きして作る方法」も紹介
リンクで飛ぶ


インフォメーションへ戻る 第1回 PHPのコードとHTMLタグ コラム
  第2回 HTML_QuickFormを使ってみよう
  第3回 HTML_QuickFormを使ってみよう(パート2) HTML_QuickForm Memo
  第4回 Smartyを使ってみよう HTML_QuickForm Menu
  第5回 HTML_QuickFormとSmartyを使ってみよう HTML_QuickForm Smarty Tips
  第6回 HTML_QuickFormとSmartyを使ってみよう(パート2) Mojavi Memo
  第7回 実践編 HTML_QuickFormとSmartyを使ってみてどうよ? Ajax Memo
  第8回 HTML_QuickForm_Controller大好き!
  第9回 Mojaviのまとめ
  第10回 Ajax使ってみました
  第11回 PHPEclipse開発環境設定
  第12回 Eclipse+PHPIDEインストール記
  第13回目 PHPIDEによるデバック
  第14回目 Selenium IDEを使ってみました
  第15回目 PDT(旧 PHPIDE)のインストール、デバック