mediaプラグイン応用(7) 複数アップロード禁止
デフォルトだと、1つのモデルに複数のファイルをアップロード可能なので、1つしかアップロードできないように改修しました。
controllerでアップロード処理済みの場合のチェックを追加し、編集画面で使用するelementを改修しました。
コントローラの改修
$this->data['Attachment'][0]=ファイルアップロードデータ
$this->data['Attachment'][1以降]=アップロード済みファイルの削除フォームデータ
なので、
- $this->data['Attachment']が3個以上の場合
あるいは
- 現在のデータ($user)にAttachmentの設定がある場合
- $this->data['Attachment'][0]が空ではない
- $this->data['Attachment'][1]['delete']が1ではない(削除チェックされてない)
場合にエラーにする処理を追加します。
app/controllers/users_controller.php
@@ -79,7 +80,18 @@ class UsersController extends ModuleController { function edit_image() { + $user = $this->User->read(null, $id); + if (!empty($this->data)) { + // 重複アップロードチェック + if (count($this->data['Attachment']) > 2) { + $this->Session->setFlash(__('Invalid data.', true)); + $this->redirect(array('action'=>'edit_image')); + } + if (count($user['Attachment']) == 1) { + if (!empty($this->data['Attachment'][0])) { + $this->Session->setFlash(__('Invalid data.', true)); + $this->redirect(array('action'=>'edit_image')); + } + if (!isset($this->data['Attachment'][1]['delete']) || ($this->data['Attachment'][1]['delete'] != '1')) { + $this->Session->setFlash(__('Check Release to delete Image.', true)); + $this->redirect(array('action'=>'edit_image')); + }
View(element)の変更
Attachmentエレメントはアップロード済みのファイルがあっても追加フォームを表示し、OFFにする設定がありません。
attachments.phpを元に、アップロード済みファイルがない場合は追加フォーム、ある場合は削除フォームを表示するエレメントを作成しました。
app/plugins/media/views/elements/medium_edit.ctp
<?php /** * ファイルアップロードフォーム * Attachmentエレメントのカスタマイズ版 * ファイルアップロード済みの場合、追加アップロードフォームを表示しない */ // アップロード済みファイルの表示サイズ if (!isset($previewVersion)) { $previewVersion = 'l'; } /* Set $assocAlias and $model if you're using this element multiple times in one form */ if (!isset($assocAlias)) { $assocAlias = 'Attachment'; } else { $assocAlias = Inflector::singularize($assocAlias); } if (!isset($model)) { $model = $form->model(); } $modelId = $form->value($form->model().'.id'); if (isset($this->data[$assocAlias][0]['basename'])) { array_unshift($this->data[$assocAlias], array()); } ?> <div class="attachments element"> <?php if (!isset($this->data[$assocAlias][1]['id']) || isset($this->data[$assocAlias][0]['file'])): ?> <!-- New Attachment --> <div class="new"> <?php echo $form->hidden($assocAlias . '.0.model', array('value' => $model)); echo $form->hidden($assocAlias . '.0.group', array('value' => strtolower($assocAlias))); echo $form->input($assocAlias . '.0.file', array( 'label' => __('File', true), 'type' => 'file', 'error' => array( 'error' => __('An error occured while transferring the file.', true), 'resource' => __('The file is invalid.', true), 'access' => __('The file cannot be processed.', true), 'location' => __('The file cannot be transferred from or to location.', true), 'permission' => __('Executable files cannot be uploaded.', true), 'size' => __('The file is too large.', true), 'pixels' => __('The file is too large.', true), 'extension' => __('The file has the wrong extension.', true), 'mimeType' => __('The file has the wrong MIME type.', true), ))); echo $form->input($assocAlias . '.0.alternative', array( 'label' => __('Textual replacement', true), 'value' => '', 'error' => __('A textual replacement must be provided.', true) )); ?> </div> <?php else: ?> <!-- Existing Attachments --> <div class="existing"> <div> <?php $i = 1; $item = $this->data[$assocAlias][$i]; echo $form->hidden($assocAlias . '.' . $i . '.id', array('value' => $item['id'])); /* echo $form->hidden($assocAlias . '.' . $i . '.alternative', array('value' => $item['alternative'])); */ $file = $upfile->file( 'filter/'.$previewVersion, $this->data[$assocAlias], $i, array( 'model_name' => $model, 'mime_type' => 'original', 'nodata' => 'image', ) ); if (!empty($file)) { echo $medium->embed( $file, array( 'alt' => $user['User']['name'], 'class' => 'shadow', 'restrict' => array('image') ) ); } // ファイル情報出力 echo $upfile->fileInfo($file, $item); echo $form->input($assocAlias . '.' . $i . '.delete', array( 'label' => __('Release', true), 'type' => 'checkbox', 'value' => 0 )); ?> </div> </div> <?php endif ?> </div>