Piece アクションクラス 2

前回のエントリに引続き、今回も Piece アクションクラスについてです。アプリケーションを作成する上で役に立つヘルパー基底クラスを準備するにあたって、じゃあ実際アクションクラスで何をやっているのかを再確認していきます。

今回は基本に戻り、「入力」→「確認」→「完了」という簡単なウィザードステップを伴うアプリケーションを、3種類のテンプレート (PHP, Smarty, HTML_Template_Flexy) を使った場合どのような違いが出るかというところを確認してみましょう。先のエントリより明らかにこっちを先に示すべきだな ;)


まずこのアプリケーションの流れを示すフローファイルです。これはどのテンプレートを使用したとしても変わることのない共通な内容です。

firstState: DisplayForm

lastState:
  name: DisplayFinish
  view: Finish

viewState:
  - name: DisplayForm
    view: Form
    activity:
      method: setupForm
    transition:
      - event: confirmForm
        nextState: ProcessConfirmForm
        action:
          method: validate

  - name: DisplayConfirmation
    view: Confirmation
    activity:
      method: setupConfirmation
    transition:
      - event: register
        nextState: ProcessRegister
        action:
          method: register
      - event: back
        nextState: DisplayForm

actionState:
  - name: ProcessConfirmForm
    transition:
      - event: goDisplayConfirmation
        nextState: DisplayConfirmation
      - event: goDisplayFormFrom
        nextState: DisplayForm

  - name: ProcessRegister
    transition:
      - event: goDisplayFinish
        nextState: DisplayFinish

続いてアクションクラスです。PHP ファイルおよび Smarty をテンプレートとして使用した場合のアクションクラスは以下のようになります。
テンプレートに関わるポイントをマークアップしてみました。ここではアクションクラスの $_user オブジェクトをエレメントとして SET しています。

<?php
require_once 'Piece/Flow/Action.php';

class SampleFormAction extends Piece_Flow_Action
{
    var $_user;

    function validate()
    {
        $this->_user = &new stdClass();

        $validation = &$this->_payload->getValidation();
        if ($validation->validate('SampleForm', $this->_user)) {
            return 'goDisplayConfirmation';
        } else {
            return 'goDisplayForm';
        }
    }

    function register()
    {
        return 'goDisplayFinish';
    }


    function setupForm()
    {
        $viewElement = &$this->_payload->getViewElement();
        $viewElement->setElementByRef('user', $this->_user);
    }

    function setupConfirmation()
    {
        $viewElement = &$this->_payload->getViewElement();
        $viewElement->setElementByRef('user', $this->_user);
    }
}
?>

テンプレートを先に示したいところですがちょこっと我慢して、続いて HTML_Template_Flexy を使用した場合におけるアクションクラスです。マークアップ部分の違いをご覧ください。
これらは配布されているパッケージに付属しているサンプルにも使用されているコードです。先の2種類のテンプレートと根本的な構造が異なっていて、FormElement を作成するための処理の一部 (実際の Element の作成は Piece 付属の Renderer_Flexy クラスが行います) を行っています。

<?php
require_once 'Piece/Flow/Action.php';

class SampleFormFlexyAction extends Piece_Flow_Action
{
    var $_user;

    function validate()
    {
        $this->_user = &new stdClass();

        $validation = &$this->_payload->getValidation();
        if ($validation->validate('SampleForm', $this->_user)) {
            return 'goDisplayConfirmation';
        } else {
            return 'goDisplayForm';
        }
    }

    function register()
    {
        return 'goDisplayFinish';
    }

    function setupForm()
    {
        $this->_setupFormAttributes();

        $fields = $this->_getFormFields();
        $elements = $this->_getFormElements();
        foreach ($fields as $field) {
            $elements[$field]['_value'] = @$this->_user->$field;
        }

        $viewElement = &$this->_payload->getViewElement();
        $viewElement->setElement('_elements', $elements);
    }

    function setupConfirmation()
    {
        $viewElement = &$this->_payload->getViewElement();
        $viewElement->setElementByRef('user', $this->_user);
    }

    function _getFormFields()
    {
        $fields = array('first_name', 'last_name');
        return $fields;
    }

    function _setupFormAttributes()
    {
        $view = $this->_flow->getView();
        $elements = $this->_getFormElements();
        $elements[$view]['_attributes']['action'] = $this->_payload->getScriptName();
        $elements[$view]['_attributes']['method'] = 'post';
        $viewElement = &$this->_payload->getViewElement();
        $viewElement->setElement('_elements', $elements);
    }

    function _getFormElements()
    {
        $viewElement = &$this->_payload->getViewElement();
        if (!$viewElement->hasElement('_elements')) {
            $elements = array();
        } else {
            $elements = $viewElement->getElement('_elements');
        }

        return $elements;
    }
}
?>

実際のテンプレートの内容は、各シーンに合わせて3つまとめてご覧ください。複雑なことは何もやっていませんが、特徴の一部を垣間見ることができるはずです。

Form (入力)

PHP
<html>
<head>
</head>
<body>
<h1>Form (PHP)</h1>
<form name="Form" id="Form">
  <input type="hidden" name="<?php echo $__flowExecutionTicketKey; ?>" value="<?php echo $__flowExecutionTicket; ?>" />

  <p>
    First Name: <input type="text" name="first_name" id="first_name" value="<?php echo @$user->first_name ? $user->first_name : '';  ?>" />
  </p>

  <p>
    Last Name: <input type="text" name="last_name" id="last_name"  value="<?php echo @$user->last_name ? $user->last_name : '';  ?>" />
  </p>

  <input type="submit" name="<?php echo $__eventNameKey; ?>_confirmForm" value="confirm" />
</form>

</body>
</html>
Smarty
<html>
<head>
</head>
<body>
<h1>Form (Smarty)</h1>
<form name="Form" id="Form">
  <input type="hidden" name="{$__flowExecutionTicketKey}" value="{$__flowExecutionTicket}" />

  <p>
    First Name: <input type="text" name="first_name" id="first_name" value="{$user->first_name}" />
  </p>

  <p>
    Last Name: <input type="text" name="last_name" id="last_name" value="{$user->last_name}" />
  </p>

  <input type="submit" name="{$__eventNameKey}_confirmForm" value="confirm" />
</form>

</body>
</html>
Flexy
<html>
<head>
</head>
<body>
<h1>Form (Flexy)</h1>
<form name="Form" id="Form">
  <input type="hidden" name="{__flowExecutionTicketKey}" value="{__flowExecutionTicket}" />

  <p>
    First Name: <input type="text" name="first_name" id="first_name" />
  </p>

  <p>
    Last Name: <input type="text" name="last_name" id="last_name" />
  </p>

  <input type="submit" name="{__eventNameKey}_confirmForm" value="confirm" />
</form>

</body>
</html>

Confirmation (確認)

PHP
<html>
<head>
</head>
<body>

<h1>Confirmation (PHP)</h1>

<p>First Name: <?php echo $user->first_name; ?></p>
<p>Last Name: <?php echo $user->last_name; ?></p>

<form name="Confirmation">
  <input type="hidden" name="<?php echo $__flowExecutionTicketKey; ?>" value="<?php echo $__flowExecutionTicket; ?>" />
  <p>
    <input type="submit" name="<?php echo $__eventNameKey; ?>_back" value="reedit" />
    <input type="submit" name="<?php echo $__eventNameKey; ?>_register" value="register" />
  </p>
</form>

</body>
</html>
Smarty
<html>
<head>
</head>
<body>

<h1>Confirmation (Smarty)</h1>

<p>First Name: {$user->first_name}</p>
<p>Last Name: {$user->last_name}</p>

<form name="Confirmation">
  <input type="hidden" name="{$__flowExecutionTicketKey}" value="{$__flowExecutionTicket}" />
  <p>
    <input type="submit" name="{$__eventNameKey}_back" value="reedit" />
    <input type="submit" name="{$__eventNameKey}_register" value="register" />
  </p>
</form>

</body>
</html>
Flexy
<html>
<head>
</head>
<body>

<h1>Confirmation (Flexy)</h1>

<p>First Name: {user.first_name}</p>
<p>Last Name: {user.last_name}</p>

<form name="Confirmation">
  <input type="hidden" name="{__flowExecutionTicketKey}" value="{__flowExecutionTicket}" />
  <p>
    <input type="submit" name="{__eventNameKey}_back" value="reedit" />
    <input type="submit" name="{__eventNameKey}_register" value="register" />
  </p>
</form>

</body>
</html>

Finish (完了)

全テンプレート共通 (何もしてない)
<html>
<head>
</head>
<body>

<h1>Finish</h1>

<p>Finished!</p>

</body>
</html>

実際に何らかのアクションクラスのヘルパーを準備するのであれば、HTML_Template_Flexy 用のサポートメソッドを持つクラスを別途ある方が良いのかもしれませんね。

トラックバック(0)

このブログ記事を参照しているブログ一覧: Piece アクションクラス 2

このブログ記事に対するトラックバックURL: http://hatotech.org/mt-admin/mt-tb.cgi/645

コメントする

このブログ記事について

このページは、が2007年2月23日 16:13に書いたブログ記事です。

ひとつ前のブログ記事は「Piece アクションクラス」です。

次のブログ記事は「ゲームキューブはお子様向けとか言ってた奴ちょっと来い」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。

Powered by Movable Type 4.01