- 2008-05-08 (木) 7:00
- Zend_Gdata
intro
前回作成したGoogleスプレッドシートを利用した追加/更新/削除/閲覧コントローラを使用して実際に画面を作成します。ソースコードはこちらからダウンロードできます。
また、デモサイトより動作を確認できます。
また、デモサイトより動作を確認できます。
1.bootstrap.php
以下のように
環境に合わせて
application/bootstrap.phpファイルを作成します。環境に合わせて
yourBaseUrlを適切に設定してください。
<?php set_include_path('../library' . PATH_SEPARATOR . get_include_path()); define('APP_BASE', '../application'); define('CONFIG_PATH', APP_BASE . '/config.ini'); require_once 'Zend/Controller/Front.php'; require_once 'Zend/Layout.php'; require_once 'Zend/Db.php'; require_once 'Zend/Config/Ini.php'; require_once 'Zend/Db/Table/Abstract.php'; $layout = Zend_Layout::startMvc(); //$layout->getView()->baseUrl = '/yourBaseUrl'; $config = new Zend_Config_Ini(CONFIG_PATH, 'staging'); $params = $config->database->params->toArray(); $params['options'][Zend_Db::CASE_FOLDING] = Zend_Db::CASE_LOWER; $dbAdapter = Zend_Db::factory($config->database->adapter, $params); Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter); Zend_Controller_Front::run(APP_BASE . '/controllers');
この記事では以下の形式のワークシートを使用しました。
No,Title,Description 1,Creating Google Spreadsheet,How to create documents 2,Reading Google Spreadsheet,How to read documents 3,Writing Google Spreadsheet,How to write documents
2.モデルの作成
以下のように
application/models/form/GsheetForm.phpファイルを作成します。
<?php require_once 'Zend/Form.php'; class GSheetForm extends Zend_Form { public function __construct() { parent::__construct(); $no = $this->createElement('text', 'no'); $no->setLabel('No') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('int'); $title = $this->createElement('text', 'title'); $title->setLabel('Title') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,100)); $description = $this->createElement('text', 'description'); $description->setLabel('Description') ->setRequired(true) ->addFilter('stringTrim') ->addValidator('stringLength', false, array(1,200)); $this->addElements(array( $no, $title, $description )); } }
3.コントローラの作成
以下のように
前回作成した
application/constorllers/GsheetsController.phpファイルを作成します。前回作成した
My_Controller_Gsheetsクラスを継承しています。
<?php require_once 'My/Controller/Gsheets.php'; require_once APP_BASE . '/models/forms/GSheetForm.php'; class GsheetsController extends My_Controller_Gsheets { protected $_session_name = "GSheets"; protected $_table_class = null; protected $_form_class = "GSheetForm"; protected $_spreadsheet_name = 'Test Spreadsheet'; public function preDispatch() { $this->view->name = "gsheets"; } public function indexAction() { $this->_helper->redirector('logon'); } }
4.ビューの作成
以下のように
application/views/scripts/gsheets/logon.phtmlファイルを作成します。
<h1><?= $this->translate($this->name . '.title.logon') ?></h1> <?php if (isset($this->googleUri)) : ?> Go <a href="<?= $this->googleUri ?>">Google Logon Page</a> <?php else : ?> <ul> <li><a href="<?= $this->url(array('action'=>'list'))?>">list spreadsheet</a></li> </ul> <div> <a href="<?= $this->url(array('action'=>'logout'))?>">Logout</a> </div> <?php endif; ?>
以下のようにapplication/views/scripts/gsheets/list.phtmlファイルを作成します。
<h1><?= $this->translate($this->name . '.title.list') ?></h1> <a href="<?=$this->url(array('action'=>'add'))?>"> <?= $this->translate($this->name . '.label.new') ?></a> <?php if ($this->list) : ?> <table> <tr> <?php $headerData = $this->list->entries[1]->getCustom(); ?> <?php foreach($headerData as $customEntry) : ?> <th><?= $customEntry->getColumnName() ?></th> <?php endforeach; ?> <th><?= $this->translate('detail') ?></th> <th><?= $this->translate('edit') ?></th> <th><?= $this->translate('delete') ?></th> </tr> <?php $row_id = 1; ?> <?php foreach($this->list->entries as $entry) : ?> <tr> <?php $rowData = $entry->getCustom(); ?> <?php foreach($rowData as $customEntry) : ?> <td><?= $customEntry->getText() ?></td> <?php endforeach; ?> <td><a href="<?=$this->url(array('action'=>'detail'))?>?id=<?=$row_id?>"> <?= $this->translate('detail') ?></a></td> <td><a href="<?=$this->url(array('action'=>'update'))?>?id=<?=$row_id?>"> <?= $this->translate('edit') ?></a></td> <td><a href="<?=$this->url(array('action'=>'delete'))?>?id=<?=$row_id?>"> <?= $this->translate('delete') ?></a></td> </tr> <?php $row_id++; ?> <?php endforeach; ?> </table> <?php endif; ?> <div> <a href="<?= $this->url(array('action'=>'index'))?>">Back</a> </div>
以下のようにapplication/views/scripts/gsheets/detail.phtmlファイルを作成します。
<h1><?= $this->translate($this->name . '.title.detail') ?></h1> <?php if ($this->values) : ?> <dl> <?php foreach($this->values as $key => $value) : ?> <dt><?= $this->translate($key) ?></dt> <dd><?= $this->escape($value) ?></dd> <?php endforeach; ?> </dl> <?php endif; ?> <?php if ($this->form) : ?> <?= $this->form ?> <?php endif; ?>
以下のようにapplication/views/scripts/gsheets/form.phtmlファイルを作成します。
<h1><?= $this->translate($this->name . '.title.' . $this->action) ?></h1> <?= $this->form ?>
以下のようにapplication/views/scripts/gsheets/finish.phtmlファイルを作成します。
<h1><?= $this->translate($this->name . '.title.finish.' . $this->action) ?></h1> <a href="<?=$this->url(array('action'=>'list'))?>"> <?= $this->translate('back') ?></a>
5.Check
Webサーバにアクセスして複数ページフォーム対応の追加/更新/削除/閲覧機能が実装されていることを確認してみてください。
履歴
| 日付 | 内容 |
|---|---|
| 2008/5/8 | 公開 |
Comments:0
Trackbacks:0
- Trackback URL for this entry
- http://www.oplabo.jp/article/43/trackback
- Listed below are links to weblogs that reference
- Zend_Gdata_Spreadsheetsを利用した追加/更新/削除/閲覧画面の実装 from Open Programming Laboratory