上松です。
管理画面へのページ追加について、4回目になります。
データの編集・削除・Massactionについてです。
・データ編集
3回目で新規登録したデータを編集します。
新規登録で使用したものに手を加えて変更もできるようにします。
Gridクラス
app/code/local/Others/Another/Block/Adminhtml/Example/Grid.php
class Others_Another_Block_Adminhtml_Example_Grid extends Mage_Adminhtml_Block_Widget_Grid { ..... public function getRowUrl($row) { return $this->getUrl('*/*/edit', array('id' => $row->getId())); } ..... }
編集ページへのURLを設定します。
データ一覧の行をクリックすると、設定したURLへ遷移します。
フォームクラス
app/code/local/Others/Another/Block/Adminhtml/Example/Edit/Form.php
class Others_Another_Block_Adminhtml_Example_Edit_Form extends Mage_Adminhtml_Block_Widget_Form { protected function _prepareForm() { $data = array(); if (Mage::registry('others_another_data')) { $data = Mage::registry('others_another_data'); } ...... $form->setValues($data); return parent::_prepareForm(); } ...... }
フォームクラスにデータの読み込みを追加します。
setValues()でデータを設定することで、あらかじめフォームにデータが入力されている状態になります。
コントローラ
app/code/local/Others/Another/controllers/Adiminhtml/ExampleController.php
class Others_Another_Adminhtml_ExampleController extends Mage_Adminhtml_Controller_Action { ...... public function newAction() { $this->_forward('edit'); } public function editAction() { $id = $this->getRequest()->getParam('id', null); $model = Mage::getModel('others_another_m/example'); if ($id) { $model->load((int) $id); if (!$model->getId()) { Mage::getSingleton('adminhtml/session')-> addError(Mage::helper('others_another_hp')->__('Example does not exust')); $this->_redirect('*/*/'); } } Mage::register('others_another_data', $model); $this->loadLayout(); $this->renderLayout(); } public function saveAction() { if ($data = $this->getRequest()->getPost()) { $model = Mage::getModel('others_another_m/example'); $id = $this->getRequest()->getParam('id'); if ($id) { $model->load($id); } $model->setData($data); try { if ($id) { $model->setId($id); } $model->save(); if (!$model->getId()) { Mage::throwException('error saving example'); } Mage::getSingleton('adminhtml/session')->addSuccess('example wasu success!'); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); if ($model && $model->getId()) { $this->_redirect('*/*/edit', array('id' => $model->getId())); } } } else { Mage::getSingleton('adminhtml/session')->addError('no data found to save'); } $this->_redirect('*/*/'); } ...... }
新規登録と編集は同じような処理が多いので共通にします。
パラメータにIDがあった場合は変更、なければ新規というように扱います。
・データの削除
コントローラ
app/code/local/Others/Another/controllers/Adiminhtml/ExampleController.php
class Others_Another_Adminhtml_ExampleController extends Mage_Adminhtml_Controller_Action { ..... public function deleteAction() { if ($id = $this->getRequest()->getParam('id')) { try { $model = Mage::getModel('others_another_m/example'); $model->setId($id); $model->delete(); Mage::getSingleton('adminhtml/session')->addSuccess('The example has been deleted...'); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); $this->_redirect('*/*/edit', array('id' => $id)); } } else { Mage::getSingleton('adminhtml/session')->addError('Unable to find the example to delete.'); } $this->_redirect('*/*/'); } ..... }
フォームコンテナの親クラスの__construct()内で削除ボタンは追加されるので
コントローラにアクションクラスを追加するだけです。
・Massactionの追加
一覧の左に表示されるチェックボックスを使って、
複数のデータに一度に同じ処理を行いたいときなどに使用します。
Gridクラス
app/code/local/Others/Another/Block/Adminhtml/Example/Grid.php
class Others_Another_Block_Adminhtml_Example_Grid extends Mage_Adminhtml_Block_Widget_Grid { ..... protected function _prepareMassaction() { $this->setMassactionIdField('id'); ・・・ コントローラに渡すフィールド $this->getMassactionBlock()->setFormFieldName('ids'); ・・・ フィールドのパラメータ名 $this->getMassactionBlock()->setUseSelectAll(false); ・・・ すべて選択する」「すべて非表示にする」リンクの表示/非表示 $this->getMassactionBlock()->addItem('delete_example', array( 'label' => 'delete', 'url' => $this->getUrl('*/*/deletes'), )); ・・・ massactionの追加 return $this; } ..... }
Gridクラス内で_prepareMassaction()をオーバーライドしてセレクトボックスにアクションを追加します。
コントローラ
app/code/local/Others/Another/controllers/Adiminhtml/ExampleController.php
class Others_Another_Adminhtml_ExampleController extends Mage_Adminhtml_Controller_Action { ..... public function deletesAction() { $exampleIds = $this->getRequest()->getPost('ids', array()); ・・・ パラメータを取得 $countDeletes = 0; foreach ($exampleIds as $id) { $model = Mage::getModel('others_another_m/example')->load($id); $model->delete(); $countDeletes++; } if ($countDeletes) { $this->_getSession()->addSuccess($this->__('%s example(s) have been delete.', $countDeletes)); } $this->_redirect('*/*/'); } ..... }
massactionに追加したURLに対応したアクションメソッドを追加します。
この場合は一括削除の処理を追加しました。
1回目から4回目までで管理側の一覧表示画面での処理を一通りまとめました。
フロント側と違い、管理側は必要な機能や画面がすでに用意されているので
手順さえ知っていれば画面の追加などが容易にできると思います。
逆にいうと、制限が多いのでそこから発展させようと思うとロジックや処理内容などを十分な理解が必要です。
また、XMLでの設定が多いので構成やスペルミスなどにも注意する必要があります。
最後までお付き合いいただいて、ありがとうございました。
参考URL
http://codemagento.com/2011/02/grids-and-forms-in-the-admin-panel/