OSDN Git Service

キャラクター選択でプロフない時のバグ修正
[trpgtools-onweb/cake-frame.git] / app / controllers / characters_controller.php
index 8d84959..3aeef4f 100644 (file)
@@ -1,4 +1,16 @@
 <?php
+/*
+ * PHP version 5
+ *
+ * @copyright Copyright 2010, Cake. (http://trpgtools-onweb.sourceforge.jp/)
+ * @category Controller
+ * @package  TRPG Data Bank
+ * @version  beta
+ * @author   Cake <cake_67@users.sourceforge.jp>
+ * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
+ * @link     http://trpgtools-onweb.sourceforge.jp/
+ */
+
 
 class CharactersController extends AppController {
 
@@ -12,43 +24,83 @@ class CharactersController extends AppController {
        var $view = 'View';
        var $theme = null;
 
-       /* ACL */
-       // 追加アクション用 crudMap
-       var $actionMapPlus = array(
-               'add_milti_profiles' => 'update',
-               'change_system' => 'update',
-               'set_status' => 'update',
-       );
+       var $isNpc = false;
 
        var $disableTokenActions = array();
        var $post_data = array();
 
+       // 検索対象項目
+       var $search_cols = array(
+       );
+
        /* メソッド */
 
        function beforeFilter() {
 
                parent::beforeFilter();
 
+               // Cache
+               $this->cacheAction = array(
+                       'index' => Configure::read('Cache.expireShort'),
+               );
+
                // 認証なしアクセス可
                $this->AuthPlus->allow('view');
                $this->AuthPlus->allow('index');
+               $this->AuthPlus->allow('search');
+
+               // 設定
+               $this->set_public_flag4view();
+               $this->set_status4view();
        }
 
        function beforeRender()
        {
                parent::beforeRender();
-
-               $this->set_public_flag4view();
-               $this->set_status4view();
        }
 
 
        /* アクションメソッド */
 
        function index($id = null) {
+               if (!empty($id) && $id == $this->user_id) {
+                       $this->redirect(array_merge(array('action'=>'mycharacter'), $this->params['named']));
+               }
+
+               $this->set('isOwner', false);
+
                $this->_index($id);
        }
 
+       function search($id = null) {
+               if (!empty($id) && $id == $this->user_id) {
+                       $this->redirect(array_merge(array('action'=>'mysearch'), $this->params['named']));
+               }
+
+               $this->set('isOwner', false);
+
+               $this->_search($id);
+       }
+
+       function mysearch(){
+               $this->set('isOwner', true);
+
+               $this->_search($this->user_id);
+       }
+
+       function mycharacter(){
+               // お知らせ
+               $news = array();
+               if (empty($this->site_configs['Site.myHome']['value'])) {
+                       $news = $this->get_news();
+               }
+               $this->set('news', $news);
+
+               $this->set('isOwner', true);
+
+               $this->_index($this->user_id);
+       }
+
        function view($id = null) {
                if (!$id) {
                        $this->Session->setFlash(__('Invalid Character.', true));
@@ -82,19 +134,42 @@ class CharactersController extends AppController {
        }
 
        function add() {
-               $systems = $this->_get_systems();
+               $systems = $this->_get_systems('public');
+               // SingleSystem
+               if (isset($this->site_configs['System.singleSystem']['value']) && $this->site_configs['System.singleSystem']['value']) {
+                       $this->params['named']['system_id'] = key($systems);
+               }
+
+               if (isset($this->params['named']['system_id']) && !empty($this->params['named']['system_id'])) {
+                       $system = $this->Character->System->find('first', array(
+                               'conditions' => array(
+                                       'System.id' => $this->params['named']['system_id'],
+                               ),
+                               'recursive' => -1,
+                               'fields' => array(
+                                       'System.id',
+                                       'System.name',
+                                       'System.public_flag',
+                                       'System.set_npc',
+                               )
+                       ));
+                       if (empty($system) || !$this->check_public_flag2($system['System'])) {
+                               $this->Session->setFlash(__('Invalid ID.', true));
+                               $this->redirect(array('action'=>'index'));
+                       }
+               }
+/*             if (isset($system['System']) && !empty($system['System']) && $system['System']['set_npc']) {
+                       $this->isNpc = true;
+               }*/
 
                if (!empty($this->data)) {
                        $this->data['Character']['user_id'] = $this->user_id;
+                       if (isset($this->site_configs['System.singleSystem']['value']) && $this->site_configs['System.singleSystem']['value']) {
+                               $this->data['Character']['system_id'] = key($systems);
+                       }
 
                        $this->Character->create();
                        if ($this->Character->save($this->data, array('fieldList' => $this->Character->fields['add']))) {
-                                       Cache::clear();
-
-                                       // Archives保存
-                                       if (!isset($this->data['CharacterProfileArchive']['trivial']) || $this->data['CharacterProfileArchive']['trivial'] != 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('controller' => 'characters', 'action'=>'view', $this->Character->id));
@@ -105,6 +180,8 @@ class CharactersController extends AppController {
 
                $this->set('systems', $systems);
                $this->set('isOwner', false);
+
+               $this->set('title_for_layout', " ". __('Add Character', true));
        }
 
        function edit($id = null) {
@@ -114,10 +191,11 @@ class CharactersController extends AppController {
                }
 
                // Characterデータ取得
-               $this->Character->System->hasMany['Profile']['fields'] = '';
+               unset($this->Character->System->hasMany['Profile']['fields']);
                $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
+               unset($this->Character->hasMany['CharacterProfileArchive']);
 
-               $character = $this->_get_character($id, array(), false, array('System' => 'Ptofile'));
+               $character = $this->_get_character($id, array(), false);
                if (!$this->isOwner($character['Character'], $this->user_id)) {
                        $this->Session->setFlash(__('No Permission', true));
                        $this->redirect(array('action'=>'index'));
@@ -127,6 +205,9 @@ class CharactersController extends AppController {
                if (!$this->check_public_flag($character['System'])) {
                        $this->redirect(array('action'=>'change_system', $id));
                }
+/*             if ($character['System']['set_npc']) {
+                       $this->isNpc = true;
+               }*/
 
                if (empty($this->data)) {
                        $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
@@ -134,70 +215,119 @@ class CharactersController extends AppController {
 
                if (!empty($this->data)) {
                        $this->post_data = $this->data;
+
+                       $this->data['clearCache'] = array(
+                               'character_id' => $id,
+                               'user_id' => $this->user_id,
+                               'system_id' => $character['System']['id'],
+                       );
+
                        // 新hasProfile処理
-                       $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
+                       if (isset($this->data['CharactersHasProfile'])) {
+                               $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
+                       }
 
                        /* validate */
                        $this->_set_validate4characters_has_profile($character['System']['id']);
-                       if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
 
-                               // 現在のhas_profiles削除
-                               $this->Character->CharactersHasProfile->deleteAll(array(
-                                       'CharactersHasProfile.character_id' => $id
-                               ));
-
-                               /* データ保存 */
-                               $this->data['Character']['id'] = $id;
-                               $this->Character->create();
+                       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.character_id' => $id
+                                       ));
+
+                                       /* データ保存 */
+                                       $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']
-                                        )
-                               ))) {
-                                       Cache::clear();
-
-                                       // Archives保存
-                                       if (!isset($this->data['CharacterProfileArchive']['trivial']) || $this->data['CharacterProfileArchive']['trivial'] != 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));
                                }
+                       } else {
+                               $validate_error = current(current(current($this->Character->validationErrors)));
+                               $this->Session->setFlash($validate_error);
                        }
 
-                       $this->data = $character;
+                       $this->data = array_merge($character, $this->post_data);
                        $this->data['Character'] = $this->post_data['Character'];
+                       $this->data['Character']['main_picture'] = $character['Character']['main_picture'];
+                       $this->data['Character']['system_id'] = $character['Character']['system_id'];
                        $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], (array)$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->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
        }
 
        function add_milti_profiles($id = null) {
@@ -209,6 +339,7 @@ class CharactersController extends AppController {
                // Characterデータ取得
                $this->Character->System->hasMany['Profile']['fields'] = '';
                $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
+               unset($this->Character->hasMany['CharacterProfileArchive']);
 
                $character = $this->_get_character($id);
                if (!$this->isOwner($character['Character'], $this->user_id)) {
@@ -228,79 +359,104 @@ 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->post_data = $this->data;
+                       $this->data['clearCache'] = array(
+                               'character_id' => $id,
+                               'user_id' => $this->user_id,
+                               'system_id' => $character['System']['id'],
+                       );
 
-                       $this->data['Character'] = $character['Character'];
+                       $this->post_data = $this->data;
+                       $this->data = array();
+                       if (isset($this->post_data['Character'])) {
+                               $this->data['Character'] = array_merge($character['Character'], $this->post_data['Character']);
+                       } else {
+                               $this->data['Character'] = $character['Character'];
+                       }
                        $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
                        $this->data['Character']['__Token'] = $this->post_data['Character']['__Token'];
                        $this->data['Character']['modified'] = null;
 
                        // 新hasProfile処理
-                       $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
-
+                       $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->post_data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
                        /* 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']
-                                        )
-                               ))) {
-                                       Cache::clear();
-
-                                       // Archives保存
-                                       if (!isset($this->data['CharacterProfileArchive']['trivial']) || $this->data['CharacterProfileArchive']['trivial'] != 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']));
-                                       Cache::clear();
-                                       $this->redirect(array('action'=>'view', $id));
+                               } else {
                                }
+                       } else {
+                               $validate_error = current(current(current($this->Character->validationErrors)));
+                               $this->Session->setFlash($validate_error);
                        }
 
-                       $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);
+
+               if (isset($this->params['named']['num'])) {
+                       $form_nums = $this->params['named']['num'];
+               } else {
+                       $form_nums = 3;
+               }
+               $this->set('form_nums', $form_nums);
+
+               $this->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
        }
 
        function change_system($id = null) {
@@ -318,11 +474,15 @@ class CharactersController extends AppController {
                        $this->Session->setFlash(__('No Permission', true));
                        $this->redirect(array('action'=>'index'));
                }
+
                // Systemチェック
                // Systemあり
                if (isset($character['System']) && !empty($character['System'])) {
+                       // 「その他」設定
+                       if ($this->isOtherSystem($character['System']['id'])) {
+                               $this->set('isChange', true);
                        // 公開状態は変更不可
-                       if ($this->check_public_flag($character['System'])) {
+                       } elseif ($this->check_public_flag($character['System'])) {
                                $this->redirect(array('action'=>'edit', $id));
                        // System非公開
                        } else {
@@ -331,6 +491,12 @@ class CharactersController extends AppController {
                }
 
                if (!empty($this->data)) {
+                       $this->data['clearCache'] = array(
+                               'character_id' => $id,
+                               'user_id' => $this->user_id,
+                               'system_id' => $character['System']['id'],
+                       );
+
                        $this->data['Character']['id'] = $id;
 
                        // validateはsystem_idのみ
@@ -351,7 +517,6 @@ class CharactersController extends AppController {
                                )
                        )) {
                                $this->Session->setFlash(__('The new system has been saved', true));
-                               Cache::clear();
                                $this->redirect(array('action'=>'view', $id));
                        } else {
                                $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
@@ -361,8 +526,10 @@ class CharactersController extends AppController {
                $this->set('isOwner', true);
                $this->set('character', $character);
 
-               $systems = $this->_get_systems();
+               $systems = $this->_get_systems('public');
                $this->set('systems', $systems);
+
+               $this->set('title_for_layout', " ". sprintf(__('%s Change System', true), $character['Character']['name']));
        }
 
 
@@ -392,10 +559,17 @@ class CharactersController extends AppController {
                        }
                        if (!$new_character_picture) {
                                $this->Session->setFlash(__('Invalid data.', true));
-                               $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
+                               $this->redirect(array('controller' => 'character_pictures', 'action'=>'listview', $id));
                        }
                }
 
+
+               $this->data['clearCache'] = array(
+                       'character_id' => $id,
+                       'user_id' => $this->user_id,
+                       'system_id' => $character['Character']['system_id'],
+               );
+
                // 設定変更
                $this->Character->id = $id;
                $this->data['Character'][$this->params['named']['mode']] = $new_character_picture;
@@ -408,8 +582,11 @@ class CharactersController extends AppController {
                        )
                );
 
+               $this->Character->deleteCache4CharacterPicture($id);
+
+
                $this->Session->setFlash(__('CharacterPicture Configuration has been saved.', true));
-               $this->redirect(array('controller' => 'character_pictures', 'action' => 'index', $id));
+               $this->redirect(array('controller' => 'character_pictures', 'action' => 'listview', $id));
        }
 
        function delete($id = null) {
@@ -418,29 +595,27 @@ class CharactersController extends AppController {
                        $this->redirect(array('action'=>'index'));
                }
 
-               $character = $this->Character->read(null, $id);
-               if (empty($character)) {
-                       $this->Session->setFlash(__('No Character', true));
-                       $this->redirect(array('action'=>'index'));
+               if ($this->_delete($id)) {
+                       $this->redirect(array('controller' => 'users', 'action'=>'index'));
                }
 
-               if (!$this->isOwner($character['Character'], $this->user_id)) {
-                       $this->Session->setFlash(__('No Permission', true));
-                       $this->redirect(array('action'=>'index'));
-               }
+               $this->set('title_for_layout', " - ". __('Delete Character', true));
+       }
 
-               if ($this->Character->del($id)) {
-                       $this->Session->setFlash(__('Character deleted', true));
-                       $this->redirect(array('action'=>'index'));
-               }
+       function admin_index($id = null) {
 
-               $this->set('isOwner', true);
+               $this->_index($id, array('isAdmin' => true));
        }
 
+       function admin_search($id = null) {
+               $this->search_cols['Character']['public_flag'] = array(
+                       'name' => __('Public Flag', true),
+                       'type' => 'select',
+                       'options' => CorePlus::set_publicflag($this->model_public_flags, array('all')),
+                       'default' => 'public',
+               );
 
-       function admin_index($id = null) {
-               unset($this->Character->belongsTo['System']['conditions']['System.public_flag']);
-               $this->_index($id, array('isAdmin' => true));
+               $this->_search($id, array('isAdmin' => true));
        }
 
        function admin_view($id = null) {
@@ -454,15 +629,84 @@ class CharactersController extends AppController {
                $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
        }
 
+       function admin_edit($id = null) {
+               if (!$id && empty($this->data)) {
+                       $this->Session->setFlash(__('Invalid ID.', true));
+                       $this->redirect(array('action'=>'index'));
+               }
+
+               // Characterデータ取得
+               $this->Character->System->hasMany['Profile']['fields'] = '';
+               $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
+               unset($this->Character->hasMany['CharacterProfileArchive']);
+
+               $character = $this->_get_character($id, array(), true);
+               if (!$character) {
+                       $this->Session->setFlash(__('No Character', true));
+                       $this->redirect(array('action'=>'index'));
+               }
+
+               if (empty($this->data)) {
+                       $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
+               }
+
+               if (!empty($this->data)) {
+
+                       $this->data['clearCache'] = array(
+                               'character_id' => $id,
+                               'user_id' => $this->user_id,
+                               'system_id' => $character['System']['id'],
+                       );
+
+                       if (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_private') {
+                               $this->data['Character']['public_flag'] = 'private';
+                       } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'set_public') {
+                               $this->data['Character']['public_flag'] = 'public';
+                       } elseif (isset($this->data['Character']['mode']) && $this->data['Character']['mode'] == 'delete_confirm') {
+                               $this->redirect(array('controller' => 'characters', 'action'=>'admin_delete', $id));
+                       } else {
+                               $this->Session->setFlash(sprintf(__('Invalid data.', true), $this->data['Character']['name']));
+                               $this->redirect(array('action'=>'view', $id));
+                       }
+
+                       $this->data['Character']['id'] = $id;
+                       $this->Character->create();
+                       if ($this->Character->save($this->data, array(
+                               'validate' => true,
+                               'fieldList' => array(
+                                       'public_flag',
+                               ),
+                       ))) {
+
+                                       $this->Session->setFlash(sprintf(__('%s has been saved.', true), $character['Character']['name']));
+                                       $this->redirect(array('action'=>'view', $id));
+                               }
+
+                       $this->data = array_merge($character, $this->data);
+                       $this->data['Character']['id'] = $id;
+               }
+
+               if (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->set('title_for_layout', " ". sprintf(__('Edit %s', true), $character['Character']['name']));
+       }
+
        function admin_delete($id = null) {
                if (!$id) {
                        $this->Session->setFlash(__('Invalid id for Character', true));
                        $this->redirect(array('action'=>'index'));
                }
-               if ($this->Character->del($id)) {
-                       $this->Session->setFlash(__('Character deleted', true));
+
+               if ($this->_delete($id, array(), true)) {
                        $this->redirect(array('action'=>'index'));
                }
+
+               $this->set('title_for_layout', " - ". __('Delete Character', true));
        }
 
 
@@ -470,41 +714,79 @@ class CharactersController extends AppController {
        function _index($id, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1) {
                if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
                        $isAdmin = true;
+                       unset($this->Character->belongsTo['System']['conditions']['System.public_flag']);
                } else {
                        $isAdmin = false;
                }
-               unset($conditions['isAdmin']);
+
+               $title = __('List of All Characters', true);
 
                // System指定分岐
                if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
                        //情報取得
-                       $conditions['System.id'] = $this->params['named']['system'];
-                       if ($isAdmin === false) {
-                               $conditions['System.public_flag'] = 'public';
-                       }
+                       $this_system = $this->_getThisSystem($this->params['named']['system'], $isAdmin);
 
-                       $this_system = $this->Character->System->find('first', array(
-                               'conditions' => $conditions,
+                       $conditions['Character.system_id'] = $this->params['named']['system'];
+
+                       // Profileの一覧表示
+                       $profiles = $this->Character->System->Profile->find('all', array(
+                               'fields' => array(
+                                       'Profile.id',
+                                       'Profile.name',
+                                       'Profile.key_name',
+                                       'Profile.profile_type',
+                                       'Profile.sort_order',
+                               ),
+                               'conditions' => array(
+                                       'Profile.system_id' => $this->params['named']['system'],
+                                       'Profile.show_list' => true,
+                               ),
                                'recursive' => -1,
                        ));
+                       $prof = array();
+                       foreach ($profiles as $k => $v) {
+                               if (isset($v['Profile']['sort_order'])  && !empty($v['Profile']['sort_order'])) {
+                                       $sort_order_profile[$v['Profile']['id']] = $v['Profile']['sort_order'];
+                               } else {
+                                       $sort_order_profile[$v['Profile']['id']] = 0;
+                               }
+
+                               $prof[$v['Profile']['id']] = array(
+                                       'id' => $v['Profile']['id'],
+                                       'name' => $v['Profile']['name'],
+                                       'key_name' => $v['Profile']['key_name'],
+                                       'profile_type' => $v['Profile']['profile_type'],
+                               );
+                       }
+                       if (!empty($sort_order_profile)) {
+                               $prof = $this->sort4sort_order($prof, $sort_order_profile);
+                       }
 
-                       if (!isset($this_system['System'])) {
-                               $this->Session->setFlash(__('Invalid System.', true));
-                               $this->redirect(array('controller' => 'systems', 'action' => 'index'));
+                       foreach ($prof as $k => $v) {
+                               $this->showlist_cols['Profile'][] = $v['id'];
                        }
 
+                       $this_system['Profile'] = $prof;
+
+                       $this_system = $this->_restore_html_system($this_system);
+
+/*                     if ($this_system['System']['set_npc']) {
+                               $this->isNpc = true;
+                       }*/
+
+                       $title = $this_system['System']['name']. " ". $title;
+
                        $this->set('this_system', $this_system);
                } else {
                        $contain = array_merge(array('System'), (array)$contain);
+
+//                     $this->isNpc = true;
                }
 
-               $isOwner = false;
                $user = array();
-
-               $conditions = array('User.id' => $id);
                if (!empty($id)) {
                        $user = $this->Character->User->find('first', array(
-                               'conditions' => $conditions,
+                               'conditions' => array('User.id' => $id),
                                'recursive' => -1,
                                'fields' => array(
                                        'User.id',
@@ -516,47 +798,277 @@ class CharactersController extends AppController {
                                $this->Session->setFlash(__('Invalid Id.', true));
                                $this->redirect(array('action'=>'index'));
                        }
+
+                       $conditions['Character.user_id'] = $id;
+                       if ($id == $this->user_id) {
+                               unset($this->paginate['conditions']['Character.public_flag']);
+                       }
+
+                       $title = $user['User']['name']. " ". $title;
                } else {
                        $contain = array_merge($contain, array('User'));
                }
                $this->set('userSet', $user);
 
-               if (!empty($user)) {
-                       if ($user['User']['id'] == $this->user_id) {
-                               $isOwner = true;
+               // Status分岐
+               if (!isset($this->params['named']['status'])) {
+                       $status = 'active';
+               } else {
+                       $status = $this->params['named']['status'];
+               }
+               switch ($status) {
+                       case 'active':
+                       case 'inactive':
+//                     case 'npc':
+                               $conditions['Character.status'] = $status;
+                               break;
+                       default:
+                               $status = 'all';
+                               unset($conditions['Character.status']);
+                               break;
+               }
+               $this->set('selected_status', $status);
+
+               $characters = $this->HtmlEscape->nl_unescape($this->_get_characters_page4user_id($id, $conditions, $limit, $fields, $contain, $order, $page));
+
+               $this->set('characters', $characters);
+
+               $this->set('title_for_layout', " - ". $title);
+       }
+
+       function _search($id, $conditions = array(), $limit = 20) {
+               if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
+                       $isAdmin = true;
+               } else {
+                       $isAdmin = false;
+               }
+
+               if (!isset($this->search_cols['Character'])) {
+                       $this->search_cols['Character'] = array();
+               }
+
+               $system_conditions['System.public_flag'] = 'public';
+               if (isset($this->data['Character']['keyword']['system_id'])) {
+                       if (!empty($this->data['Character']['keyword']['system_id'])) {
+                               $this->passedArgs['system'] = $this->data['Character']['keyword']['system_id']['value'];
+                               $system_id = $this->passedArgs['system'];
                        }
+               } elseif (isset($this->passedArgs['system'])) {
+                       $system_id = $this->passedArgs['system'];
+               } else {
+                       $system_id = 0;
+               }
+               $systems = $this->_get_systems('public');
+               $systems =
+                       array(0 => __('All', true)) + $systems;
+
+               $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
+                       'system_id' => array(
+                               'name' => __('System', true),
+                               'type' => 'select',
+                               'options' => $systems,
+                               'default' => $system_id,
+                       ),
+               ));
+
+               if (isset($this->passedArgs['status'])) {
+                       $status = $this->passedArgs['status'];
+               } else {
+                       $status = 'active';
                }
 
-               $conditions = array();
-               if (!empty($id)) {
-                       $conditions['Character.user_id'] = $id;
+               $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
+                       'status' => array(
+                               'name' => __('Status', true),
+                               'type' => 'select',
+                               'options' => CorePlus::set_status($this->model_status2),
+                               'default' => $status,
+                       ),
+               ));
+
+               $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
+                       'name' => array(
+                               'name' => __('Charater Name', true),
+                               'type' => 'text',
+                       ),
+               ));
+               if ($this->site_configs['Character.NotesSearch']['value']) {
+                       $this->search_cols['Character'] = array_merge($this->search_cols['Character'], array(
+                               'notes' => array(
+                                       'name' => __('Notes', true),
+                                       'type' => 'text',
+                               )
+                       ));
                }
-               if ($isAdmin === true) {
-                       $conditions['isAdmin'] = true;
+
+               // Profileの検索対象
+               if (!empty($system_id)) {
+                       $this_system = $this->_getThisSystem($system_id, $isAdmin);
+
+                       $profiles = $this->Character->System->Profile->find('all', array(
+                               'fields' => array(
+                                       'Profile.id',
+                                       'Profile.name',
+                                       'Profile.key_name',
+                                       'Profile.profile_type',
+                                       'Profile.sort_order',
+                               ),
+                               'conditions' => array(
+                                       'Profile.system_id' => $system_id,
+                                       'Profile.search' => true,
+                               ),
+                               'recursive' => -1,
+                       ));
+                       $prof = array();
+                       foreach ($profiles as $k => $v) {
+                               if (isset($v['Profile']['sort_order'])  && !empty($v['Profile']['sort_order'])) {
+                                       $sort_order_profile[$v['Profile']['id']] = $v['Profile']['sort_order'];
+                               } else {
+                                       $sort_order_profile[$v['Profile']['id']] = 0;
+                               }
+
+                               $prof[$v['Profile']['id']] = array(
+                                       'id' => $v['Profile']['id'],
+                                       'name' => $v['Profile']['name'],
+//                                     'key_name' => $v['Profile']['key_name'],
+//                                     'profile_type' => $v['Profile']['profile_type'],
+                               );
+                       }
+                       if (!empty($sort_order_profile)) {
+                               $prof = $this->sort4sort_order($prof, $sort_order_profile);
+                       }
+
+                       foreach ($prof as $k => $v) {
+                               $this->search_cols['CharactersHasProfile'][$v['id']] = array(
+                                       'name' => $v['name'],
+                                       'type' => 'text',
+                               );
+                       }
                }
-               if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
-                       $conditions['Character.system_id'] = $this->params['named']['system'];
+
+               if (!empty($id) && ($this->user['User']['id'] == $id)) {
+                       $isAdmin = true;
                }
-                $characters =  $this->HtmlEscape->nl_unescape($this->_get_characters_page4user_id($id, $conditions, $limit, $fields, $contain, $order, $page));
 
-               $this->set('characters', $characters);
+               $this->set('search_cols', $this->search_cols);
 
-               $this->set('isOwner', false);
-               $this->set('isOwner_userSet', $isOwner);
+               if ($this->site_configs['Character.ListSearchNotes']['value'] || $this->site_configs['Character.NotesSearch']['value']) {
+                       $this->fields = array_merge($this->fields, array('Character.notes'));
+               }
+//             $this->set('fields', $this->fields);
+
+               // 検索条件設定
+               $type = 'OR';
+
+               $page = 1;
+               if (!empty($this->data)) {
+                       if (isset($this->data['Character']['page']) && $this->data['Character']['page'] > 0) {
+                               $page = $this->data['Character']['page'];
+                               unset($this->data['Character']['page']);
+                       }
+
+                       $prev_type = 'AND';
+                       $prev_type_set = false;
+                       $new_condition = null;
+                       foreach ($this->data as $model => $search_conditions) {
+                               $keywords = array();
+                               if (isset($search_conditions['keyword']) && !empty($search_conditions['keyword'])) {
+                                       foreach ($search_conditions['keyword'] as $field => $search_options) {
+                                               switch($model) {
+                                                       case 'Character':
+                                                               if ($field == 'public_flag') {
+                                                                       if (isset($conditions['isAdmin']) && !empty($conditions['isAdmin']) && $search_options['value'] == 'all') {
+                                                                               unset($this->paginate['conditions']['Character.public_flag']);
+                                                                       } else {
+                                                                               $conditions['Character.public_flag'] = $search_options['value'];
+                                                                               $conditions['public_force'] = true;
+                                                                       }
+                                                               } elseif ($field == 'system_id') {
+                                                                       if (empty($search_options['value'])) {
+                                                                               if (isset($this->params['named']['system'])) {
+                                                                                       unset($this->params['named']['system']);
+                                                                               }
+                                                                       } else {
+                                                                               $this->params['named']['system'] = $search_options['value'];
+                                                                       }
+                                                               } elseif ($field == 'status') {
+                                                                       if ($search_options['value'] == 'all') {
+                                                                               $this->params['named']['status'] = 'all';
+                                                                       } else {
+                                                                               $this->params['named']['status'] = $search_options['value'];
+                                                                       }
+                                                               } else {
+                                                                       if (isset($search_options['value']) && !empty($search_options['value'])) {
+                                                                               if (!empty($prev_type_set)) {
+                                                                                       $new_condition .= ' '. strtoupper($prev_type). ' ';
+                                                                                       $prev_type_set = false;
+                                                                               }
+                                                                               if (isset($search_options['type']) && in_array(strtolower($search_options['type']), array('and', 'or'))) {
+                                                                                       $prev_type = strtoupper($search_options['type']);
+                                                                                       $prev_type_set = true;
+                                                                               }
+
+                                                                               $new_condition .= $this->_create_search_sql($search_options['value'], $field, $model);
+                                                                       }
+                                                               }
+                                                               break;
+                                                       case 'CharactersHasProfile':
+                                                               if (isset($search_options['value']) && !empty($search_options['value'])) {
+                                                                       $conditions['profile_search'] = true;
+
+                                                                       if (!empty($prev_type_set)) {
+                                                                               $new_condition .= ' '. strtoupper($prev_type). ' ';
+                                                                               $prev_type_set = false;
+                                                                       }
+                                                                       if (isset($search_options['type']) && in_array(strtolower($search_options['type']), array('and', 'or'))) {
+                                                                               $prev_type = strtoupper($search_options['type']);
+                                                                               $prev_type_set = true;
+                                                                       }
+
+                                                                       $new_condition .= $this->_create_search_sql($search_options['value'], 'value', $model, $field);
+                                                               }
+                                                               break;
+                                                       default: 
+                                                               $this->Session->setFlash(__('Invalid Data.', true));
+                                                               $this->redirect(array('action'=>'index'));
+                                                               break;
+                                               }
+                                       }
+                               }
+                       }
+
+                       if (empty($isAdmin)) {
+                               if (!empty($new_condition)) {
+                                       $new_condition = '('. $new_condition. ')';
+                               }
+                               if (isset($conditions['profile_search'])) {
+                                       $new_condition .= ' AND';
+                                       $new_condition .= ' CharactersHasProfile.public_flag = \'public\'';
+                               }
+                       }
+
+                       if (!empty($new_condition)) {
+                               $conditions['AND'][] = '('. $new_condition. ')';
+                       }
+               }
+
+               $this->_index($id, $conditions, $limit, array(), array(), array(), $page);
        }
 
        function _view($id, $conditions = array(), $isAdmin = false) {
-               // スキン設定
-               if (isset($this->params['named']['mode']) && !empty($this->params['named']['mode'])) {
-                        $this->view = 'Theme';
-                        $this->theme = $this->params['named']['mode'];
-               }
+               $this->helpers[] = 'CharacterSheet';
 
                // キャラデータ取得
                $orig_character = $this->_get_character($id, $conditions, $isAdmin);
-
                $character = $this->Character->set_profiles2view($orig_character);
 
+               Configure::write('isOwner', false);
+               if ($isAdmin || CorePlus::isOwner($character['Character'], $this->user_id)) {
+                       Configure::write('isOwner', true);
+               }
+
                // Systemチェック
                $this->set('systemValid', 'public');
                if (!$character['System']) {
@@ -565,10 +1077,147 @@ class CharactersController extends AppController {
                if (!$this->check_public_flag($character['System'])) {
                        $this->set('systemValid', 'unpublic');
                }
+/*             if ($character['System']['set_npc']) {
+                       $this->isNpc = true;
+               }*/
+               $character = $this->_restore_html_system($character);
+               $character['System']['Profile'] = $this->_restore_html_characters_has_profiles($character['System']['Profile'], false);
+
+               $this->set('title_for_layout', " - ". $character['Character']['name']);
+
+               // キャラクターシート設定
+               if (isset($this->params['named']['mode']) && !empty($this->params['named']['mode'])) {
+                       $mode = $this->params['named']['mode'];
+
+                       // スキン公開チェック
+                       $public_flg = false;
+                       $skin = array();
+                       if (!$isAdmin) {
+                               // 公開済み
+                               foreach ($character['System']['CharacterSheet'] as $v) {
+                                       if ($v['key_name'] == $mode) {
+                                               $public_flg = true;
+                                               $skin = $v;
+                                               break;
+                                       }
+                               }
+
+                               // 自身のキャラシのみ
+                               if (!$public_flg) {
+                                       $this->CharacterSheet = CorePlus::set_model('CharacterSheet');
+                                       $characterSheet = $this->CharacterSheet->find('first', array(
+                                               'conditions' => array('CharacterSheet.key_name' => $mode),
+                                               'recursive' => -1,
+                                       ));
+
+                                       if ($this->_checkCharaSheeOwner($characterSheet)) {
+                                               $skin = $characterSheet['CharacterSheet'];
+                                               $public_flg = true;
+                                       }
+                               }
+                       } else {
+                               $public_flg = true;
+                       }
+                       if ($public_flg) {
+                               $this->view = 'Theme';
+                               $this->theme = $mode;
+
+                               $this->set('skin', $skin);
+                       }
+               }
 
                return $character;
        } 
 
+       function _delete($id = null, $conditions = array(), $isAdmin = false) {
+               if (!$id) {
+                       $this->Session->setFlash(__('Invalid id for Character', true));
+                       $this->redirect(array('action'=>'index'));
+               }
+
+               $character = $this->_view($id, $conditions, $isAdmin);
+               if (empty($character)) {
+                       $this->Session->setFlash(__('No Character', true));
+                       $this->redirect(array('action'=>'index'));
+               }
+               $this->set('character', $character);
+
+               if (!$isAdmin && !$this->isOwner($character['Character'], $this->user_id)) {
+                       $this->Session->setFlash(__('No Permission', true));
+                       $this->redirect(array('action'=>'index'));
+               }
+               $this->set('isOwner', true);
+
+               if (!empty($this->data)) {
+
+                       $this->data['clearCache'] = array(
+                               'character_id' => $id,
+                               'user_id' => $this->user_id,
+                               'system_id' => $character['System']['id'],
+                       );
+
+                       if ($this->data['Character']['confirm'] == 'yes') {
+                               // Character
+                               $this->data['Character']['id'] = $id;
+                               $this->data['Character']['main_picture'] = null;
+                               $this->data['Character']['full_length'] = null;
+                               $this->data['Character']['deleted'] = true;
+                               $this->data['Character']['deleted_date'] = date('Y-m-d H:i:s');
+
+                               $this->Character->create();
+                               if ($this->Character->save(
+                                       $this->data,
+                                       array(
+                                               'validate' => false,
+                                               'fieldList' => array(
+                                                       'main_picture',
+                                                       'full_length',
+                                                       'deleted',
+                                                       'deleted_date'
+                                               ),
+                                       )
+                               )) {
+
+                                       // CharactersHasProfiles
+                                       $this->Character->CharactersHasProfile->updateAll(
+                                               array(
+                                                       'CharactersHasProfile.public_flag' => "'private'",
+                                               ),
+                                               array(
+                                                       'CharactersHasProfile.character_id' => $id
+                                               )
+                                       );
+
+                                       // CharacterProfileArchives
+                                       $date = date('Y-m-d H:i:s');
+                                       $this->Character->CharacterProfileArchive->updateAll(
+                                               array(
+                                                       'CharacterProfileArchive.deleted' => true,
+                                                       'CharacterProfileArchive.deleted_date' => "'$date'",
+                                               ),
+                                               array(
+                                                       'CharacterProfileArchive.character_id' => $id
+                                               )
+                                       );
+
+                                       // Attachments
+                                       if (isset($character['CharacterPicture']) && !empty($character['CharacterPicture'])) {
+                                               foreach ($character['CharacterPicture'] as $picture) {
+                                                       $this->Character->CharacterPicture->delete($picture['id']);
+                                               }
+                                       }
+
+                                       $this->Session->setFlash(__('Character deleted', true));
+
+                                       return true;
+                               } else {
+                                       $this->Session->setFlash(__('The Character could not be deleted.', true));
+                               }
+                       }
+               }
+
+               return false;
+       }
 
        /* 共通関数 */
        function _get_character($id, $conditions = array(), $isAdmin = false)
@@ -585,6 +1234,10 @@ class CharactersController extends AppController {
                        $this->redirect(array('action'=>'index'));
                }
 
+               if (isset($character['System']['Profile'])) {
+                       $character['System'] = $this->_restore_html_profile($character['System']);
+               }
+
                return $character;
        }
 
@@ -599,6 +1252,7 @@ class CharactersController extends AppController {
                        if (empty($v['ProfileTable'])) {
                                continue;
                        }
+
                        $tmp = array();
                        $rows = count($v['CharactersHasProfile']);
                        for($i=0; $i<$rows; $i++) {
@@ -615,117 +1269,14 @@ class CharactersController extends AppController {
                                }
                        }
                        $profile[$k]['CharactersHasProfile'] = $tmp;
-                       }
-
-               return $profile;
-       }
-
-       /* POSTのCharactersHasProfileを処理 */
-       function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
-       {
-               $prev_profile_id = null;
-               $profileTable_tmp = array();
-               $i = 0;
-               foreach ($characters_has_profiles as $k => $v) {
-                       if (!empty($character_id)) {
-                               $characters_has_profiles[$k]['character_id'] = $character_id;
-                       }
-                       // 配列valueの処理
-                       if (is_array($v['value'])) {
-                               $characters_has_profiles[$k]['value'] = $v['value'][0];
-                       }
-
-                       // サニタイズ
-                       $characters_has_profiles[$k]['value'] = Sanitize::html($characters_has_profiles[$k]['value']);
-                       $characters_has_profiles[$k]['value'] = preg_replace('/[\\\n]/', '', $characters_has_profiles[$k]['value']);
-                       $characters_has_profiles[$k]['value'] = preg_replace('/[\\\r]/', '', $characters_has_profiles[$k]['value']);
-                       // textarea改行処理
-                       if (isset($v['is_textarea']) && $v['is_textarea'] == 1) {
-                               $characters_has_profiles[$k]['value'] = str_replace(array("\n\r", '\n', "\r"), '<br />', $characters_has_profiles[$k]['value']);
-                               $characters_has_profiles[$k]['value'] = str_replace("\\", '', $characters_has_profiles[$k]['value']);
-                       }
-                       $characters_has_profiles[$k]['value'] = Sanitize::stripAll($characters_has_profiles[$k]['value']);
-
-                       // 空の値処理
-                       // table: 一時保管して全項目空の場合行削除
-                       if (CorePlus::is_valid($v, 'profile_table_id')) {
-                               if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
-                                       $i++;
-                               }
-                               $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($characters_has_profiles[$k], $k);
-                       // それ以外は削除
-                       } elseif (empty($v['value'])) {
-                                       unset($characters_has_profiles[$k]);
-                       }
-               }
-
-               // table: 空行削除、不正profile_table_idデータ削除
-               if (!empty($profileTable_tmp)) {
-                       if (!isset($this->Character->ProfileTable)) {
-                               $this->Character->ProfileTable = CorePlus::set_model('ProfileTable');
-                       }
-                       foreach($profileTable_tmp as $profile_id => $v) {
-                               $profile_table_columns = $this->Character->ProfileTable->find('list', array(
-                                       'conditions' => array('ProfileTable.profile_id' => $profile_id),
-                                       'fields' => array('ProfileTable.id'),
-                                       'order' => array('ProfileTable.sort_order' => 'asc'),
-                                       'recursive' => -1,
-                               ));
-
-                               foreach ($v as $i => $profile_tables) {
-                                       // 不正POSTの削除
-                                       $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
-                                       if (!empty($diff_keys)) {
-                                               foreach($diff_keys as $dvalue) {
-                                                       unset($characters_has_profiles[$dvalue[1]]);
-                                               }
-                                       }
-
-                                       // 空行削除
-                                       $delete_flg = true;
-                                       foreach($profile_table_columns as $k => $columns) {
-                                               if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
-                                                       $delete_flg = false;
-                                                       break;
-                                               }
-                                       }
-                                       if ($delete_flg === true) {
-                                               foreach ($profile_tables as $delk) {
-                                                       unset($characters_has_profiles[$delk[1]]);
-                                               }
-                                       }
-                               }
-                       }
-               }
-
-               // 指定データのみ変更
-               if (!is_null($profile_id) && !empty($now_data)) {
-                       $tmp = array();
-                       $i = 0;
-                       foreach($now_data as $k => $v) {
-                               if ($v['id'] == $profile_id) {
-                                       $v['CharactersHasProfile'] = $characters_has_profiles;
-                               }
-
-                               foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
-                                       $tmp[$i] = $v2;
-                                       if (isset($tmp[$i]['id'])) {
-                                               unset($tmp[$i]['id']);
-                                       }
-                                       if (isset($tmp[$i]['character_id'])) {
-                                               unset($tmp[$i]['character_id']);
-                                       }
 
-                                       $i++;
-                               }
+                       if (isset($profile[$k]['ProfileTable'][0]['ProfileTableStatic']) && !empty($profile[$k]['ProfileTable'][0]['ProfileTableStatic'])) {
+                               $profile[$k]['ProfileTable'][0]['ProfileTableStatic'] = $this->_restore_html_profile_table_static($profile[$k]['ProfileTable'][0]['ProfileTableStatic']);
                        }
-
-                       $characters_has_profiles = $tmp;
                }
 
-               return $characters_has_profiles;
+               return $profile;
        }
-
        // CharactersHasProfileの追加valiadte設定
        function _set_validate4characters_has_profile($system_id)
        {
@@ -746,8 +1297,10 @@ class CharactersController extends AppController {
        function _restore_html_character($data, $nl2br = false) {
                $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
                $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
+               $data['secret_notes'] = $this->{$this->modelClass}->restore_html($data['secret_notes'], false, false, false);
                if ($nl2br) {
                        $data['notes'] = str_replace('<br />', "\n", $data['notes']);
+                       $data['secret_notes'] = str_replace('<br />', "\n", $data['secret_notes']);
                }
 
                return $data;
@@ -780,8 +1333,96 @@ 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;
+               }
+       }
+
+       function _create_search_sql($keywords, $field, $model, $profile_id = null)
+       {
+               $type = 'OR';
+               $result = null;
+
+               if (empty($keywords) || !is_string($keywords) || empty($field)) {
+                       return array();
+               }
+
+               if (!isset($this->search_cols[$model]) ||
+                       ((empty($profile_id) && !array_key_exists($field, $this->search_cols[$model]))
+                       || (!empty($profile_id) && !array_key_exists($profile_id, $this->search_cols[$model])))) {
+                       return null;
+               }
+
+               $keywords = rawurldecode($keywords);
+               $searchwords = explode(",", $keywords);
+               foreach ($searchwords as $k2 => $searchword) {
+                       if (empty($searchword)) {
+                               continue;
+                       }
+
+                       if (!empty($result)) {
+                               $result .= ' '. strtoupper($type). ' ';
+                       } else {
+                               $result .= '(';
+                       }
+
+                       $result .= '('. $model.'.'.$field. ' LIKE '. '\'%'. $searchword. '%\'';
+                       if (!empty($profile_id)) {
+                               $result .= ' AND CharactersHasProfile.profile_id = '. $profile_id;
+                       }
+                       $result .= ')';
+               }
+               $result .= ')';
+
+               return $result;
+       }
+
 }