OSDN Git Service

プロフの入力必須項目チェック
authorCake <cake_67@users.sourceforge.jp>
Mon, 15 Nov 2010 08:02:13 +0000 (17:02 +0900)
committerCake <cake_67@users.sourceforge.jp>
Mon, 15 Nov 2010 08:02:13 +0000 (17:02 +0900)
app/controllers/characters_controller.php
app/controllers/profile_selects_controller.php
app/views/helpers/profiledisp.php

index 93779e2..1065fed 100644 (file)
@@ -202,62 +202,96 @@ class CharactersController extends AppController {
                        /* validate */
                        $this->_set_validate4characters_has_profile($character['System']['id']);
                        if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
+                               // requiredのチェック
+                               $requiredCheck = $this->_checkRequiredProfile($character['System']['Profile']);
 
-                               // 現在のhas_profiles削除
-                               $this->Character->CharactersHasProfile->deleteAll(array(
-                                       'CharactersHasProfile.character_id' => $id
-                               ));
+                               if ($requiredCheck) {
+                                       // 現在のhas_profiles削除
+                                       $this->Character->CharactersHasProfile->deleteAll(array(
+                                               'CharactersHasProfile.character_id' => $id
+                                       ));
 
-                               /* データ保存 */
-                               $this->data['Character']['id'] = $id;
-                               $this->Character->create();
+                                       /* データ保存 */
+                                       $this->data['Character']['id'] = $id;
+                                       $this->Character->create();
 
-                               if ($this->Character->saveAll($this->data, array(
-                                       'validate' => false,
-                                        'fieldList' => array_merge(
-                                               $this->Character->fields['edit'], 
-                                               $this->Character->CharactersHasProfile->fields['add']
-                                        )
-                               ))) {
-
-                                       // Archives保存
-                                       if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
-                                               $this->Character->saveCharacterProfile($this->Character->id, $this->data); 
-                                       }
+                                       if ($this->Character->saveAll($this->data, array(
+                                               'validate' => false,
+                                                'fieldList' => array_merge(
+                                                       $this->Character->fields['edit'], 
+                                                       $this->Character->CharactersHasProfile->fields['add']
+                                                )
+                                       ))) {
+
+                                               // Archives保存
+                                               if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
+                                                       $this->Character->saveCharacterProfile($this->Character->id, $this->data); 
+                                               }
 
-                                       $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
-                                       $this->redirect(array('action'=>'view', $id));
+                                               $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
+                                               $this->redirect(array('action'=>'view', $id));
+                                       }
                                }
                        }
 
                        $this->data = array_merge($character, $this->data);
                        $this->data['Character'] = $this->post_data['Character'];
+                       $this->data['Character']['main_picture'] = $character['Character']['main_picture'];
                        $this->data['Character']['id'] = $id;
+                       foreach ($this->data['System']['Profile'] as $k1 => $profile) {
+                               if ($profile['profile_type'] == 'table') {
+                                       $colum_num = count($profile['CharactersHasProfile']);
+                               } elseif ($profile['profile_type'] == 's-table') {
+                                       $colum_num = count($profile['CharactersHasProfile']);
+                               } elseif ($profile['profile_type'] == 'checkbox') {
+                                       $colum_num = count($profile['ProfileSelect']);
+                               } elseif ($profile['profile_type'] == 'm-input') {
+                                       $colum_num = count($profile['CharactersHasProfile']);
+                               } else {
+                                       $colum_num = 1;
+                               }
+
+                               if ($colum_num == 0) {
+                                       if ($profile['profile_type'] == 's-table') {
+                                               $colum_num = count($profile['ProfileTable']) * count($profile['ProfileTable'][0]['ProfileTableStatic']);
+                                       } elseif ($profile['profile_type'] == 'table') {
+                                               $colum_num = count($profile['ProfileTable']) * 4;
+                                       } elseif ($profile['profile_type'] == 'checkbox') {
+                                               $colum_num = count($profile['ProfileSelect']);
+                                       } elseif ($profile['profile_type'] == 'm-input') {
+                                               $colum_num = 5;
+                                       } else {
+                                               $colum_num = 1;
+                                       }
+                               }
+
+                               for ($i=0; $i<$colum_num; $i++) {
+                                       if (!isset($profile['CharactersHasProfile'][$i])) {
+                                               $profile['CharactersHasProfile'][$i] = array('character_id' => $character['Character']['id']);
+                                       }
 
-                       foreach($this->data['System']['Profile'] as $k1 => $profile) {
-                               foreach($profile['CharactersHasProfile'] as $k2 => $v) {
                                        $postdata = array_shift($this->post_data['CharactersHasProfile']);
-                                       if (isset($v['profile_select_id']) && !isset($postdata['profile_select_id'])) {
-                                               array_unshift($this->post_data['CharactersHasProfile'], $postdata);
-                                               continue;
-                                       } 
+
                                        if (is_array($postdata['value'])) {
                                                $postdata['value'] = $postdata['value'][0];
                                        }
-                                       $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['value'] = $postdata['value'];
-                                       if (isset($postdata['profile_select_id'])) {
-                                               $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['profile_select_id'] = $postdata['profile_select_id'];
+
+                                       if (isset($postdata[$i])) {
+                                               $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$i] = array_merge($profile['CharactersHasProfile'][$i], $postdata[$i]);
+                                       } else {
+                                               $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$i] = array_merge($profile['CharactersHasProfile'][$i], $postdata);
                                        }
                                }
                        }
                }
 
-               if (empty($this->data)) {
+               elseif (empty($this->data)) {
                        $this->data = $character;
                        $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
 
-                       $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
                }
+               $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
+
                $this->set('isOwner', true);
 
                $this->pageTitle .= " ". sprintf(__('Edit %s', true), $character['Character']['name']);
@@ -292,25 +326,23 @@ class CharactersController extends AppController {
 
                $profile_id = $this->params['named']['profile_id'];
 
-               if (empty($this->data)) {
-                       $target = array();
-                       foreach ($character['System']['Profile'] as $profile) {
-                               if ($profile['id'] == $profile_id) {
-                                       $target = $profile;
-                                       break;
-                               }
+               $target = array();
+               foreach ($character['System']['Profile'] as $profile) {
+                       if ($profile['id'] == $profile_id) {
+                               $target = $profile;
+                               break;
                        }
+               }
 
-                       if (!$target) {
-                               $this->Session->setFlash(__('No Profile.', true));
-                               $this->redirect(array('action'=>'view', $id));
-                       }
+               if (!$target) {
+                       $this->Session->setFlash(__('No Profile.', true));
+                       $this->redirect(array('action'=>'view', $id));
+               }
 
-                       $character['System']['Profile'] = array();
-                       $character['System']['Profile'][0] = $target;
+               $character['System']['Profile'] = array();
+               $character['System']['Profile'][0] = $target;
 
-                       $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
-               }
+               $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
 
                if (!empty($this->data)) {
                        $this->data['clearCache'] = array(
@@ -332,43 +364,49 @@ class CharactersController extends AppController {
                        /* validate */
                        $this->_set_validate4characters_has_profile($character['System']['id']);
                        if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
+                               // requiredのチェック
+                               $requiredCheck = $this->_checkRequiredProfile($character['System']['Profile']);
+
+                               if ($requiredCheck) {
+                                       // 現在のhas_profiles削除
+                                       $this->Character->CharactersHasProfile->deleteAll(array(
+                                               'CharactersHasProfile.profile_id' => $target['id'],
+                                               'CharactersHasProfile.character_id' => $id
+                                       ));
+
+                                       /* データ保存 */
+                                       $this->Character->create();
+                                       if ($this->Character->saveAll($this->data, array(
+                                               'validate' => false,
+                                                'fieldList' => array_merge(
+                                                       $this->Character->CharactersHasProfile->fields['add']
+                                                )
+                                       ))) {
+
+                                               // Archives保存
+                                               if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
+                                                       $this->Character->saveCharacterProfile($this->Character->id, $this->data); 
+                                               }
 
-                               // 現在のhas_profiles削除
-                               $this->Character->CharactersHasProfile->deleteAll(array(
-                                       'CharactersHasProfile.character_id' => $id
-                               ));
-
-                               /* データ保存 */
-                               $this->Character->create();
-                               if ($this->Character->saveAll($this->data, array(
-                                       'validate' => false,
-                                        'fieldList' => array_merge(
-                                               $this->Character->CharactersHasProfile->fields['add']
-                                        )
-                               ))) {
-
-                                       // Archives保存
-                                       if (isset($this->data['Character']['archive']) && $this->data['Character']['archive'] == 1) {
-                                               $this->Character->saveCharacterProfile($this->Character->id, $this->data); 
+                                               $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
+                                               $this->redirect(array('action'=>'view', $id));
+                                       } else {
+                                               $this->data = array_merge($character, $this->data);
                                        }
-
-                                       $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
-                                       $this->redirect(array('action'=>'view', $id));
                                } else {
-                                       $this->data = array_merge($character, $this->data);
                                }
                        }
 
-                       $this->data = $character;
-                       $this->data['Character'] = $this->post_data['Character'];
+                       $this->data = array_merge($character, $this->data);
                        $this->data['Character']['id'] = $id;
+                       unset($this->data['Character']['__Token']);
 
                }
 
                if (empty($this->data)) {
                        $this->data = $character;
-                       $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
                }
+               $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
 
                $this->set('isOwner', true);
 
@@ -1090,4 +1128,54 @@ class CharactersController extends AppController {
                return $data;
        }
 
+       function _checkRequiredProfile($profile)
+       {
+               if (empty($profile) || !isset($this->data['CharactersHasProfile'])) {
+                       return true;
+               }
+
+               $requireCheck = array();
+               foreach($this->data['CharactersHasProfile'] as $k => $v) {
+                       if (!isset($requireCheck[$v['profile_id']])) {
+                               $requireCheck[$v['profile_id']] = null;
+                       }
+
+                       if (isset($requireCheck[$v['profile_id']]['value']) && !empty($requireCheck[$v['profile_id']]['value'])) {
+                               continue;
+                       }
+
+                       if (empty($v['value'])) {
+                               continue;
+                       }
+
+                       if (isset($v['profile_table_static_id']) && !empty($v['profile_table_static_id'])) {
+                               if (!isset($requireCheck[$v['profile_id']]['profile_table_id']) || empty($requireCheck[$v['profile_id']]['profile_table_id'])) {
+                                       $requireCheck[$v['profile_id']]['profile_table_id'] = $v['profile_table_id'];
+                               }
+
+                               if ($requireCheck[$v['profile_id']]['profile_table_id'] == $v['profile_table_id']) {
+                                       continue;
+                               }
+                       }
+
+                       $requireCheck[$v['profile_id']]['value'] = $v['value'];
+               }
+
+               $errors = array();
+               foreach ($profile as $k => $v) {
+                       if ($v['required']) {
+                               if (!isset($requireCheck[$v['id']]) || empty($requireCheck[$v['id']]['value'])) {
+                                       $errors[] = $v['name'];
+                               }
+                       }
+               }
+
+               if ($errors) {
+                       $this->Session->setFlash(sprintf(__('%s is required.', true), implode(',', $errors)));
+                       return false;
+               } else {
+                       return true;
+               }
+       }
+
 }
index b701103..1d8cf7d 100644 (file)
@@ -54,7 +54,7 @@ class ProfileSelectsController extends AppController {
                                $this->check_profile_type($profileSelect);
 
                                // 削除
-                               if (!isset($v['value']) || empty($v['value'])) {
+                               if (isset($v['value']) && empty($v['value'])) {
                                        if ($this->ProfileSelect->del($id)) {
                                        } else {
                                                $this->Session->setFlash(__('Some data could not be saved. Please, try again.', true));
@@ -157,7 +157,6 @@ class ProfileSelectsController extends AppController {
                        $this->redirect(array('controller' => 'systems', 'action'=>'admin_index'));
                }
 
-               $profile = $this->ProfileSelect->Profile->read(null, $profile_id);
                $profile = $this->ProfileSelect->Profile->find('first', array(
                        'conditions' => array(
                                'Profile.id' => $profile_id,
index a25a305..9fa4b3f 100644 (file)
@@ -146,7 +146,7 @@ class ProfiledispHelper extends Helper {
 
                        if ($disp_add_link) {
                                echo $this->Html->div('editLink', $this->Html->link(
-                                       __('Edit Data', true),
+                                       __('Edit', true),
                                        array(
                                                'action' => 'add_milti_profiles',
                                                $options['character_id'].
@@ -816,7 +816,7 @@ class ProfiledispHelper extends Helper {
                        echo '</table>';
                        if ($disp_add_link) {
                                $link = $this->Html->link(
-                                       __('Edit Data', true),
+                                       __('Edit', true),
                                        array(
                                                'action' => 'add_milti_profiles',
                                                $character_id.