6月 05

19.編集・退会

go @ 5:46 AM

続けて編集画面もやってしまいます。 edit.ctp を開いて
一行目に追加

<?php echo $javascript->link(array('jquery','form','util'), false);?>

以下のフォーム開始を

<?php echo $form->create('Member');?>

以下のように変更

<?php echo $form->create('Member', array('type'=>'file')); ?>
<fieldset style="display:none;">
	<?php
		echo $form->input('img1', array('type'=>'hidden'));
		echo $form->input('img2', array('type'=>'hidden'));
		echo $form->input('id');
	?>
</fieldset>

idのhiddenをこっちに持ってきてることに注意。
なぜかと言うと、残しておくと、例のjQueryで強制的にテーブルにされてしまうのでおかしなことになってしまうのです。

以下のメインの部分は基本的に add.ctp と同じになるように修正して、

	<?php
		echo $form->input('id');
		echo $form->input('email');
		echo $form->input('password');
		echo $form->input('type_id');
		echo $form->input('birthday');
		echo $form->input('img1');
		echo $form->input('img2');
		echo $form->input('Favorite');
	?>

画像のところをちょっと変えます

		echo $form->input('solid1', array('type'=>'file', 'label' => '画像1'));
		if (!$form->error('solid1')){
			if ($this->data['Member']['img1']){
				echo '<div class="input img1"><label for="MemberImg1"></label>';
				echo $html->image("/files/".$this->data['Member']['img1']);
				echo '</div>';
			}
		}

さっきと同じでjQueryでテーブルをねつ造する関係で、画像の置き場所も一個の新しいテーブルの行にしてしまおうという作戦です。
出力されるソースはこんなになります。

<div class="input file">
	<label for="MemberSolid1">画像1</label>
	<input type="file" name="data[Member][solid1]" value="" id="MemberSolid1" />
</div>
<div class="input img1"><label for="MemberImg1"></label>
	<img src="/files/xxxxxx.jpg" alt="" />
</div>

コントローラを修正します。

	function edit() {
		if (!$this->Session->check('Member.id')) {
			$this->redirect(array('action'=>'login'));
		}
		if (!empty($this->data)) {
			$this->data['Favorite']['Favorite'] = $this->data['Member']['favorites'];
			if ($this->data['Member']['solid1']['type']){
				$this->data['Member']['img1'] = $this->_imgUpload('solid1');
			}
			if ($this->data['Member']['solid2']['type']){
				$this->data['Member']['img2'] = $this->_imgUpload('solid2');
			}
			if ($this->Member->save($this->data)) {
				$this->Session->setFlash(__('会員情報を更新しました', true));
			} else {
				$this->Session->setFlash(__('エラーがあります', true));
			}
		} else {
			$this->data = $this->Member->read(null, $this->Session->read('Member.id'));
			if ($this->data['Favorite']){
				foreach ($this->data['Favorite'] as $k => $v){
					$this->data['Member']['favorites'][] = $v['id'];
				}
			}
		}
		$favorites = $this->Member->Favorite->find('list');
		$types = $this->Member->Type->find('list');
		$this->set(compact('favorites','types'));
	}

セッションで認証するので、

	function edit($id = null) {

の引数部分はなしにしてます。
他は add の方と同じですが、
初期に編集画面へ来た時の、DBからデータを読む処理の後に、その「好きな物」データを変換する処理を入れています。上記20〜24行目。
Favorite を favorites に入れ直しています。
これにより、

		echo $form->input('favorites', array('multiple'=>'checkbox', 'label' => '好きな物'));

この書き方のビューに反映されます。

最後に退会の処理を作ります。
アクション del() のままでもいいんですが、お客さんに del ってのもどうかと思ったので、 leave にしてみました。

	function leave() {
		if (!$this->Session->check('Member.id')) {
			$this->redirect(array('action'=>'login'));
		}
		if (!empty($this->data)) {
			if ($this->Member->del($this->Session->read('Member.id'))) {
				$this->Session->destroy();
				$this->render('quit');
			} else {
				$this->Session->setFlash(__('退会できませんでした', true));
			}
		} else {
			$this->data = $this->Member->read(null, $this->Session->read('Member.id'));
			$this->set('member', $this->data);
		}
		$favorites = $this->Member->Favorite->find('list');
		$types = $this->Member->Type->find('list');
		$this->set(compact('favorites','types'));
	}

また、 view.ctp をコピーしてきて改造します。

<?php echo $javascript->link(array('jquery','util'), false); ?>

<div class="members view">
<h2><?php  __('退会します。よろしいですか?'); ?></h2>
	<table>
	<tr>
		<th><?php __('メールアドレス'); ?></th>
		<td>
			<?php echo $member['Member']['email']; ?>
			&nbsp;
		</td>
	</tr>
	<tr>
		<th><?php __('パスワード'); ?></th>
		<td>
			<?php echo $member['Member']['password']; ?>
			&nbsp;
		</td>
	</tr>
	<tr>
		<th><?php __('種別'); ?></th>
		<td>
			<?php echo $types[$member['Member']['type_id']]; ?>
			&nbsp;
		</td>
	</tr>
	<tr>
		<th><?php __('誕生日'); ?></th>
		<td>
			<?php echo $member['Member']['birthday']; ?>
			&nbsp;
		</td>
	</tr>
	<tr>
		<th><?php __('画像1'); ?></th>
		<td>
			<?php if (isset($member['Member']['img1'])){ echo $html->image("/files/".$member['Member']['img1']);} ?>
			&nbsp;
		</td>
	</tr>
	<tr>
		<th><?php __('画像2'); ?></th>
		<td>
			<?php if (isset($member['Member']['img2'])){ echo $html->image("/files/".$member['Member']['img2']);} ?>
			&nbsp;
		</td>
	</tr>
	<tr>
		<th><?php __('好きな物'); ?></th>
		<td>
			<?php if (!empty($member['Favorite'])):?>
				<ul>
				<?php foreach ($member['Favorite'] as $favorite): ?>
					<li><?php echo $favorites[$favorite['id']];?></li>
				<?php endforeach; ?>
				</ul>
			<?php endif; ?>
			&nbsp;
		</td>
	</tr>
</table>

<?php echo $form->create('Member', array('action' => 'leave')); ?>
<fieldset style="display:none;"><?php echo $form->input('id'); ?></fieldset>

<div class="submit"><?php echo $form->button('戻る',array('id'=>'back_button')); ?></div>
<?php echo $form->end('退会する'); ?>

</div>

ここで id=’back_button’ というのを作ったので、これも javascript の util.js に追加しておきます。

		$('#back_button').click(function(){
			history.back();
		});

戻れればいいだけなので、ただのヒストリーバックです。
退会の完了画面用にビューで quit.ctp を用意しておきます。

>>次のページ「Authコンポーネント」へ

Comments are closed.

here comes