OSDN Git Service

複数ファイルアップロード禁止実装
authorCake <cake_67@users.sourceforge.jp>
Mon, 22 Feb 2010 16:21:26 +0000 (01:21 +0900)
committerCake <cake_67@users.sourceforge.jp>
Mon, 22 Feb 2010 16:21:26 +0000 (01:21 +0900)
app/controllers/users_controller.php
app/plugins/media/views/elements/medium_edit.ctp [new file with mode: 0644]
app/views/users/edit_image.ctp

index 9382eb4..0cfef1a 100644 (file)
@@ -49,6 +49,7 @@ class UsersController extends ModuleController {
        }
 
        function index() {
+               $this->set('is_owner', true);
                self::_index();
        }
 
@@ -79,7 +80,20 @@ class UsersController extends ModuleController {
        function edit_image() {
                $id = $this->AuthPlus->user("id");
 
+               $user = $this->User->read(null, $id);
+
                if (!empty($this->data)) {
+                       // 重複アップロードチェック
+                       if (!empty($user['Attachment']) && 
+                               (count($user['Attachment']) > 2 
+                               || !isset($this->data['Attachment'][1]['delete']) 
+                               || ($this->data['Attachment'][1]['delete'] != '1')
+                       )) {
+                               $this->Session->setFlash(__('User is able to have the only One image.', true));
+                               $this->redirect(array('action'=>'edit_image'));
+                       }
+
+
                        $this->data['User']['id'] = $id;
 
                        $fieldList = array(
@@ -122,7 +136,7 @@ class UsersController extends ModuleController {
                        }
                }
 
-               $this->data = $this->User->read(null, $id);
+               $this->data = $user;
        }
 
        function change_password() {
@@ -149,6 +163,8 @@ class UsersController extends ModuleController {
        }
        
        function mobile_index() {
+               $this->set('is_owner', true);
+
                self::_index();
        }
        function mobile_view($id = null) {
diff --git a/app/plugins/media/views/elements/medium_edit.ctp b/app/plugins/media/views/elements/medium_edit.ctp
new file mode 100644 (file)
index 0000000..0804c06
--- /dev/null
@@ -0,0 +1,107 @@
+<?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]) || empty($this->data[$assocAlias])): ?>
+       <!-- 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 . '.model', array('value' => $model));
+                       echo $form->hidden($assocAlias . '.' . $i . '.group', array('value' => $item['group']));
+                       echo $form->hidden($assocAlias . '.' . $i . '.dirname', array('value' => $item['dirname']));
+                       echo $form->hidden($assocAlias . '.' . $i . '.basename', array('value' => $item['basename']));
+                       echo $form->hidden($assocAlias . '.' . $i . '.alternative', array('value' => $item['alternative']));
+
+                       if ($file = $upfile->file('filter/'.$previewVersion, $item, $model)) {
+                               $url = null; // 元ファイル出力実装まで仮
+
+                               echo $medium->embed($medium->file($previewVersion . '/', $item), array(
+                                       '$file' => $file,
+                                       'restrict' => array('image')
+                               ));
+
+                               $Medium = Medium::factory($file);
+                               $size = $medium->size($file);
+
+                               if (isset($number)) {
+                                       $size = $number->toReadableSize($size);
+                               } else {
+                                       $size .= ' Bytes';
+                               }
+
+                               printf('<div class="fileOptions"><em>%s</em>&nbsp;(%s/%s)</div>',
+                                               $item['alternative'],
+//                                             $url ? $html->link($item['basename'], $url) : $item['basename'],
+                                               strtolower($Medium->name), $size);
+                       }
+
+                       echo $form->input($assocAlias . '.' . $i . '.delete', array(
+                               'label' => __('Release', true),
+                               'type' => 'checkbox',
+                               'value' => 0
+                       ));
+               ?>
+               </div>
+       </div>
+       <?php endif ?>
+</div>
\ No newline at end of file
index 5f6d866..a861f02 100644 (file)
@@ -9,7 +9,7 @@
 <fieldset>
 <legend><?php __('Edit Image');?></legend>
 <?php
-       echo $this->element('attachments', array(
+       echo $this->element('medium_edit', array(
                'plugin' => 'media',
                'previewVersion' => 'm',
        ));