初めてのプラグイン フォームメール - CakePHP1.2RC2 その3設定編
設定のコントローラ
bakeでいけるかと思いつつ、なぜかプラグインの中でbakeしようとするとモデルがなんちゃらと怒られるので、appの中でbakeしてプラグインに移動後ちょこちょこっと手直し。
将来を見越して、admin routesしてみる。
app/plugin/form_mail/controllers/form_mail_elements_controller.php
<?php class FormMailElementsController extends FormMailAppController { public $name = 'FormMailElements'; function admin_add() { if (!empty($this->data)) { $this->FormMailElement->create(); if ($this->FormMailElement->save($this->data)) { $this->Session->setFlash(__('The FormMailElement has been saved', true)); $this->redirect(array( 'controller' => 'form_mail_forms', 'action' => 'view', $this->data['FormMailElement']['form_mail_form_id'], )); } else { $this->Session->setFlash(__('The FormMailElement could not be saved. Please, try again.', true)); } } else { if (!empty($this->params['named']['form'])) { $this->data['FormMailElement']['form_mail_form_id'] = $this->params['named']['form']; } else { $this->Session->setFlash(__('Invalid FormMailElement', true)); $this->redirect(array( 'controller' => 'form_mail_forms', 'action' => 'index' )); } } $types = $this->FormMailElement->types; $rules = $this->FormMailElement->rules; $this->set(compact('types', 'rules')); } function admin_edit($id = null) { if (!$id && empty($this->data)) { $this->Session->setFlash(__('Invalid FormMailElement', true)); $this->redirect(array('action' => 'index')); } if (!empty($this->data)) { if ($this->FormMailElement->save($this->data)) { $this->Session->setFlash(__('The FormMailElement has been saved', true)); $this->redirect(array( 'controller' => 'form_mail_forms', 'action' => 'view', $this->data['FormMailElement']['form_mail_form_id'], )); } else { $this->Session->setFlash(__('The FormMailElement could not be saved. Please, try again.', true)); } } if (empty($this->data)) { $this->data = $this->FormMailElement->read(null, $id); } $types = $this->FormMailElement->types; $rules = $this->FormMailElement->rules; $this->set(compact('types', 'rules')); } function admin_delete($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid id for FormMailElement', true)); $this->redirect(array( 'controller' => 'form_mail_forms', 'action' => 'index' )); } $form_id = $this->FormMailElement->field('form_mail_form_id', array('id' => $id)); if ($this->FormMailElement->del($id)) { $this->Session->setFlash(__('FormMailElement deleted', true)); $this->redirect(array( 'controller' => 'form_mail_forms', 'action' => 'view', $form_id, )); } } } ?>
app/plugin/form_mail/controllers/form_mail_forms_controller.php
<?php class FormMailFormsController extends FormMailAppController { public $name = 'FormMailForms'; public $scaffold; function admin_index() { $this->FormMailForm->recursive = 0; $this->set('formMailForms', $this->paginate()); } function admin_view($id = null) { if (!$id) { $this->flash(__('Invalid FormMailForm', true), array('action' => 'index')); } $this->set('formMailForm', $this->FormMailForm->read(null, $id)); $types = $this->FormMailForm->FormMailElement->types; $rules = $this->FormMailForm->FormMailElement->rules; $this->set(compact('types', 'rules')); } function admin_add() { if (!empty($this->data)) { $this->FormMailForm->create(); if ($this->FormMailForm->save($this->data)) { $this->flash(__('FormMailForm saved.', true), array('action' => 'index')); } else { } } } function admin_edit($id = null) { if (!$id && empty($this->data)) { $this->flash(__('Invalid FormMailForm', true), array('action' => 'index')); } if (!empty($this->data)) { if ($this->FormMailForm->save($this->data)) { $this->flash(__('The FormMailForm has been saved.', true), array('action' => 'index')); } else { } } if (empty($this->data)) { $this->data = $this->FormMailForm->read(null, $id); } } function admin_delete($id = null) { if (!$id) { $this->flash(__('Invalid FormMailForm', true), array('action' => 'index')); } if ($this->FormMailForm->del($id)) { $this->flash(__('FormMailForm deleted', true), array('action' => 'index')); } } } ?>
設定用のビュー
さくさくいきます。
app/plugin/form_mail/views/form_mail_elements/admin_add.ctp
<div class="formMailElements form"> <?php echo $form->create('FormMailElement');?> <fieldset> <legend><?php __('Add FormMailElement');?></legend> <?php echo $form->input('form_mail_form_id', array('type' => 'hidden')); echo $form->input('label'); echo $form->input('type', array('options' => $types)); echo $form->input('options'); echo $form->input('rule', array('options' => $rules, 'empty' => true)); echo $form->input('required'); ?> </fieldset> <?php echo $form->end('Submit');?> </div>
app/plugin/form_mail/views/form_mail_elements/admin_edit.ctp
<div class="formMailElements form"> <?php echo $form->create('FormMailElement');?> <fieldset> <legend><?php __('Edit FormMailElement');?></legend> <?php echo $form->input('id'); echo $form->input('label'); echo $form->input('type', array('options' => $types)); echo $form->input('options'); echo $form->input('rule', array('options' => $rules, 'empty' => true)); echo $form->input('required'); ?> </fieldset> <?php echo $form->end('Submit');?> </div>
app/plugin/form_mail/views/form_mail_forms/admin_add.ctp
<div class="formMailForms form"> <?php echo $form->create('FormMailForm');?> <fieldset> <legend><?php __('Add FormMailForm');?></legend> <?php echo $form->input('title'); echo $form->input('email'); ?> </fieldset> <?php echo $form->end('Submit');?> </div> <div class="actions"> <ul> <li><?php echo $html->link(__('List FormMailForms', true), array('action'=>'index'));?></li> </ul> </div>
app/plugin/form_mail/views/form_mail_forms/admin_edit.ctp
<div class="formMailForms form"> <?php echo $form->create('FormMailForm');?> <fieldset> <legend><?php __('Edit FormMailForm');?></legend> <?php echo $form->input('id'); echo $form->input('title'); echo $form->input('email'); ?> </fieldset> <?php echo $form->end('Submit');?> </div> <div class="actions"> <ul> <li><?php echo $html->link(__('Delete', true), array('action'=>'delete', $form->value('FormMailForm.id')), null, sprintf(__('Are you sure you want to delete # %s?', true), $form->value('FormMailForm.id'))); ?></li> <li><?php echo $html->link(__('List FormMailForms', true), array('action'=>'index'));?></li> </ul> </div>
app/plugin/form_mail/views/form_mail_forms/admin_index.ctp
<div class="formMailForms index"> <h2><?php __('FormMailForms');?></h2> <p> <?php echo $paginator->counter(array( 'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true) )); ?></p> <table cellpadding="0" cellspacing="0"> <tr> <th><?php echo $paginator->sort('id');?></th> <th><?php echo $paginator->sort('title');?></th> <th><?php echo $paginator->sort('email');?></th> <th class="actions"><?php __('Actions');?></th> </tr> <?php $i = 0; foreach ($formMailForms as $formMailForm): $class = null; if ($i++ % 2 == 0) { $class = ' class="altrow"'; } ?> <tr<?php echo $class;?>> <td> <?php echo $formMailForm['FormMailForm']['id']; ?> </td> <td> <?php echo $formMailForm['FormMailForm']['title']; ?> </td> <td> <?php echo $formMailForm['FormMailForm']['email']; ?> </td> <td class="actions"> <?php echo $html->link(__('View', true), array('action'=>'view', $formMailForm['FormMailForm']['id'])); ?> <?php echo $html->link(__('Edit', true), array('action'=>'edit', $formMailForm['FormMailForm']['id'])); ?> <?php echo $html->link(__('Delete', true), array('action'=>'delete', $formMailForm['FormMailForm']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $formMailForm['FormMailForm']['id'])); ?> </td> </tr> <?php endforeach; ?> </table> </div> <div class="paging"> <?php echo $paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));?> | <?php echo $paginator->numbers();?> <?php echo $paginator->next(__('next', true).' >>', array(), null, array('class'=>'disabled'));?> </div> <div class="actions"> <ul> <li><?php echo $html->link(__('New FormMailForm', true), array('action'=>'add')); ?></li> </ul> </div>
app/plugin/form_mail/views/form_mail_forms/admin_view.ctp
<div class="formMailForms view"> <h2><?php __('FormMailForm');?></h2> <dl><?php $i = 0; $class = ' class="altrow"';?> <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Id'); ?></dt> <dd<?php if ($i++ % 2 == 0) echo $class;?>> <?php echo $formMailForm['FormMailForm']['id']; ?> </dd> <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Title'); ?></dt> <dd<?php if ($i++ % 2 == 0) echo $class;?>> <?php echo $formMailForm['FormMailForm']['title']; ?> </dd> <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Email'); ?></dt> <dd<?php if ($i++ % 2 == 0) echo $class;?>> <?php echo $formMailForm['FormMailForm']['email']; ?> </dd> </dl> </div> <div class="actions"> <ul> <li><?php echo $html->link(__('Edit FormMailForm', true), array('action'=>'edit', $formMailForm['FormMailForm']['id'])); ?> </li> <li><?php echo $html->link(__('Delete FormMailForm', true), array('action'=>'delete', $formMailForm['FormMailForm']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $formMailForm['FormMailForm']['id'])); ?> </li> <li><?php echo $html->link(__('List FormMailForms', true), array('action'=>'index')); ?> </li> </ul> </div> <div class="related"> <h3><?php __('Related Form Mail Elements');?></h3> <?php if (!empty($formMailForm['FormMailElement'])):?> <table cellpadding = "0" cellspacing = "0"> <tr> <th><?php __('Id'); ?></th> <th><?php __('Label'); ?></th> <th><?php __('Type'); ?></th> <th><?php __('Options'); ?></th> <th><?php __('Rule'); ?></th> <th><?php __('Required'); ?></th> <th class="actions"><?php __('Actions');?></th> </tr> <?php $i = 0; foreach ($formMailForm['FormMailElement'] as $formMailElement): $class = null; if ($i++ % 2 == 0) { $class = ' class="altrow"'; } ?> <tr<?php echo $class;?>> <td><?php echo $formMailElement['id'];?></td> <td><?php echo $formMailElement['label'];?></td> <td><?php echo $types[$formMailElement['type']];?></td> <td><?php echo $formMailElement['options'];?></td> <td> <?php echo ($formMailElement['rule']) ? $rules[$formMailElement['rule']]: '';?> </td> <td> <?php echo ($formMailElement['required']) ? '必須': '';?> </td> <td class="actions"> <?php echo $html->link(__('Edit', true), array('controller'=> 'form_mail_elements', 'action'=>'edit', $formMailElement['id'])); ?> <?php echo $html->link(__('Delete', true), array('controller'=> 'form_mail_elements', 'action'=>'delete', $formMailElement['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $formMailElement['id'])); ?> </td> </tr> <?php endforeach; ?> </table> <?php endif; ?> <div class="actions"> <ul> <li><?php echo $html->link(__('New Form Mail Element', true), array('controller'=> 'form_mail_elements', 'action'=>'add', 'form' => $formMailForm['FormMailForm']['id']));?> </li> </ul> </div> </div>
その4動作編へ続く