OSDN Git Service

クォートでArchive表示できないエラーの修正
[trpgtools-onweb/cake-frame.git] / app / controllers / characters_controller.php
1 <?php
2
3 class CharactersController extends AppController {
4
5         var $name = 'Characters';
6         var $helpers = array(
7                 'Select',
8                 'Profiledisp',
9         );
10
11         /* テーマ */
12         var $view = 'View';
13         var $theme = null;
14
15         /* ACL */
16         // 追加アクション用 crudMap
17         var $actionMapPlus = array(
18                 'add_milti_profiles' => 'update',
19                 'change_system' => 'update',
20                 'set_status' => 'update',
21         );
22
23         var $disableTokenActions = array();
24         var $post_data = array();
25
26         /* メソッド */
27
28         function beforeFilter() {
29
30                 parent::beforeFilter();
31
32                 // 認証なしアクセス可
33                 $this->AuthPlus->allow('view');
34                 $this->AuthPlus->allow('index');
35         }
36
37         function beforeRender()
38         {
39                 parent::beforeRender();
40
41                 $this->set_public_flag4view();
42                 $this->set_status4view();
43         }
44
45
46         /* アクションメソッド */
47
48         function index($id = null) {
49                 $this->_index($id);
50         }
51
52         function view($id = null) {
53                 if (!$id) {
54                         $this->Session->setFlash(__('Invalid Character.', true));
55                         $this->redirect(array('action'=>'index'));
56                 }
57
58                 $character = $this->_view($id, array(), false);
59                 // Systemチェック
60                 if (!$this->check_public_flag($character['System'])) {
61                         unset($character['System']['Profile']);
62                 }
63
64                 $this->set('maxPictures', $this->check_character_picture_max(count($character['CharacterPicture'])));
65
66                 $isOwner = false;
67                 if ($this->isOwner($character['Character'], $this->user_id)) {
68                         $isOwner = true;
69                 }
70                 $this->set('isOwner', $isOwner);
71
72                 // キャラ画像のpublic_flagチェック
73                 if ($isOwner !== true && !empty($character['CharacterPicture'])) {
74                         foreach($character['CharacterPicture'] as $k => $v) {
75                                 if (!$this->check_public_flag2($v)) {
76                                         unset($character['CharacterPicture'][$k]);
77                                 }
78                         }
79                 }
80
81                 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
82         }
83
84         function add() {
85                 $systems = $this->_get_systems();
86
87                 if (!empty($this->data)) {
88                         $this->data['Character']['user_id'] = $this->user_id;
89
90                         $this->Character->create();
91                         if ($this->Character->save($this->data, array('fieldList' => $this->Character->fields['add']))) {
92                                         Cache::clear();
93
94                                         // Archives保存
95                                         if (!isset($this->data['CharacterProfileArchive']['trivial']) || $this->data['CharacterProfileArchive']['trivial'] != 1) {
96                                                 $this->Character->saveCharacterProfile($this->Character->id, $this->data); 
97                                         }
98
99                                 $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
100                                 $this->redirect(array('controller' => 'characters', 'action'=>'view', $this->Character->id));
101                         } else {
102                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
103                         }
104                 }
105
106                 $this->set('systems', $systems);
107                 $this->set('isOwner', false);
108         }
109
110         function edit($id = null) {
111                 if (!$id && empty($this->data)) {
112                         $this->Session->setFlash(__('Invalid ID.', true));
113                         $this->redirect(array('action'=>'index'));
114                 }
115
116                 // Characterデータ取得
117                 $this->Character->System->hasMany['Profile']['fields'] = '';
118                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
119
120                 $character = $this->_get_character($id, array(), false, array('System' => 'Ptofile'));
121                 if (!$this->isOwner($character['Character'], $this->user_id)) {
122                         $this->Session->setFlash(__('No Permission', true));
123                         $this->redirect(array('action'=>'index'));
124                 }
125
126                 // Systemチェック
127                 if (!$this->check_public_flag($character['System'])) {
128                         $this->redirect(array('action'=>'change_system', $id));
129                 }
130
131                 if (empty($this->data)) {
132                         $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
133                 }
134
135                 if (!empty($this->data)) {
136                         $this->post_data = $this->data;
137                         // 新hasProfile処理
138                         $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile']);
139
140                         /* validate */
141                         $this->_set_validate4characters_has_profile($character['System']['id']);
142                         if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
143
144                                 // 現在のhas_profiles削除
145                                 $this->Character->CharactersHasProfile->deleteAll(array(
146                                         'CharactersHasProfile.character_id' => $id
147                                 ));
148
149                                 /* データ保存 */
150                                 $this->data['Character']['id'] = $id;
151                                 $this->Character->create();
152
153                                 if ($this->Character->saveAll($this->data, array(
154                                         'validate' => false,
155                                          'fieldList' => array_merge(
156                                                 $this->Character->fields['edit'], 
157                                                 $this->Character->CharactersHasProfile->fields['add']
158                                          )
159                                 ))) {
160                                         Cache::clear();
161
162                                         // Archives保存
163                                         if (!isset($this->data['CharacterProfileArchive']['trivial']) || $this->data['CharacterProfileArchive']['trivial'] != 1) {
164                                                 $this->Character->saveCharacterProfile($this->Character->id, $this->data); 
165                                         }
166
167                                         $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
168                                         $this->redirect(array('action'=>'view', $id));
169                                 }
170                         }
171
172                         $this->data = $character;
173                         $this->data['Character'] = $this->post_data['Character'];
174                         $this->data['Character']['id'] = $id;
175
176                         foreach($this->data['System']['Profile'] as $k1 => $profile) {
177                                 foreach($profile['CharactersHasProfile'] as $k2 => $v) {
178                                         $postdata = array_shift($this->post_data['CharactersHasProfile']);
179                                         if (isset($v['profile_select_id']) && !isset($postdata['profile_select_id'])) {
180                                                 array_unshift($this->post_data['CharactersHasProfile'], $postdata);
181                                                 continue;
182                                         } 
183                                         if (is_array($postdata['value'])) {
184                                                 $postdata['value'] = $postdata['value'][0];
185                                         }
186                                         $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['value'] = $postdata['value'];
187                                         if (isset($postdata['profile_select_id'])) {
188                                                 $this->data['System']['Profile'][$k1]['CharactersHasProfile'][$k2]['profile_select_id'] = $postdata['profile_select_id'];
189                                         }
190                                 }
191                         }
192                 }
193
194                 if (empty($this->data)) {
195                         $this->data = $character;
196                         $this->data['Character'] = $this->_restore_html_character($this->data['Character'], true);
197
198                         $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile'], true);
199                 }
200                 $this->set('isOwner', true);
201         }
202
203         function add_milti_profiles($id = null) {
204                 if (!$id || !CorePlus::is_valid($this->params['named'], 'profile_id')) {
205                         $this->Session->setFlash(__('Invalid ID.', true));
206                         $this->redirect(array('action'=>'index'));
207                 }
208
209                 // Characterデータ取得
210                 $this->Character->System->hasMany['Profile']['fields'] = '';
211                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
212
213                 $character = $this->_get_character($id);
214                 if (!$this->isOwner($character['Character'], $this->user_id)) {
215                         $this->Session->setFlash(__('No Permission', true));
216                         $this->redirect(array('action'=>'index'));
217                 }
218                 // Systemチェック
219                 if (!$this->check_public_flag($character['System'])) {
220                         $this->redirect(array('action'=>'change_system', $id));
221                 }
222
223                 // Profilesデータ取得
224                 if (empty($character['System']['Profile'])) {
225                         $this->Session->setFlash(__('Invalid Profile.', true));
226                         $this->redirect(array('action'=>'view', $id));
227                 }
228
229                 $profile_id = $this->params['named']['profile_id'];
230
231                 if (empty($this->data)) {
232                         $target = array();
233                         foreach ($character['System']['Profile'] as $profile) {
234                                 if ($profile['id'] == $profile_id) {
235                                         $target = $profile;
236                                         break;
237                                 }
238                         }
239
240                         if (!$target) {
241                                 $this->Session->setFlash(__('No Profile.', true));
242                                 $this->redirect(array('action'=>'view', $id));
243                         }
244
245                         $character['System']['Profile'] = array();
246                         $character['System']['Profile'][0] = $target;
247
248                         $character['System']['Profile'] = $this->_set_profile_table2characters_has_profiles($character['System']['Profile']);
249                 }
250
251                 if (!empty($this->data)) {
252                         $this->post_data = $this->data;
253
254                         $this->data['Character'] = $character['Character'];
255                         $this->data['Character'] = $this->_restore_html_character($this->data['Character']);
256                         $this->data['Character']['__Token'] = $this->post_data['Character']['__Token'];
257                         $this->data['Character']['modified'] = null;
258
259                         // 新hasProfile処理
260                         $this->data['CharactersHasProfile'] = $this->_set_new_characters_has_profile($this->data['CharactersHasProfile'], $profile_id, $character['System']['Profile']);
261
262                         /* validate */
263                         $this->_set_validate4characters_has_profile($character['System']['id']);
264                         if ($this->Character->saveAll($this->data, array('validate' => 'only'))) {
265
266                                 // 現在のhas_profiles削除
267                                 $this->Character->CharactersHasProfile->deleteAll(array(
268                                         'CharactersHasProfile.character_id' => $id
269                                 ));
270
271                                 /* データ保存 */
272                                 $this->Character->create();
273                                 if ($this->Character->saveAll($this->data, array(
274                                         'validate' => false,
275                                          'fieldList' => array_merge(
276                                                 $this->Character->CharactersHasProfile->fields['add']
277                                          )
278                                 ))) {
279                                         Cache::clear();
280
281                                         // Archives保存
282                                         if (!isset($this->data['CharacterProfileArchive']['trivial']) || $this->data['CharacterProfileArchive']['trivial'] != 1) {
283                                                 $this->Character->saveCharacterProfile($this->Character->id, $this->data); 
284                                         }
285
286                                         $this->Session->setFlash(sprintf(__('%s has been saved.', true), $this->data['Character']['name']));
287                                         Cache::clear();
288                                         $this->redirect(array('action'=>'view', $id));
289                                 }
290                         }
291
292                         $this->data = $character;
293                         $this->data['Character'] = $this->post_data['Character'];
294                         $this->data['Character']['id'] = $id;
295
296                 }
297
298                 if (empty($this->data)) {
299                         $this->data = $character;
300                         $this->data['System']['Profile'] = $this->_restore_html_characters_has_profiles($this->data['System']['Profile']);
301                 }
302
303                 $this->set('isOwner', true);
304         }
305
306         function change_system($id = null) {
307                 if (!$id) {
308                         $this->Session->setFlash(__('Invalid ID.', true));
309                         $this->redirect(array('action'=>'index'));
310                 }
311
312                 // Characterデータ取得
313                 $this->Character->System->hasMany['Profile']['fields'] = '';
314                 $this->Character->System->Profile->hasMany['CharactersHasProfile']['fields'] = '';
315
316                 $character = $this->_get_character($id);
317                 if (!$this->isOwner($character['Character'], $this->user_id)) {
318                         $this->Session->setFlash(__('No Permission', true));
319                         $this->redirect(array('action'=>'index'));
320                 }
321                 // Systemチェック
322                 // Systemあり
323                 if (isset($character['System']) && !empty($character['System'])) {
324                         // 公開状態は変更不可
325                         if ($this->check_public_flag($character['System'])) {
326                                 $this->redirect(array('action'=>'edit', $id));
327                         // System非公開
328                         } else {
329                                 $this->set('isChange', true);
330                         }
331                 }
332
333                 if (!empty($this->data)) {
334                         $this->data['Character']['id'] = $id;
335
336                         // validateはsystem_idのみ
337                         $this->Cahracter['validate'] = array(
338                                 'system_id' => array(
339                                         'validSystemId' => array(
340                                                 'rule' => array('validSystemId', true),
341                                                 'allowEmpty' => false,
342                                         )
343                                 ),
344                         );
345
346                         if ($this->Character->save(
347                                 $this->data,
348                                 array(
349                                         'validate' => true,
350                                         'fieldList' => array('system_id'),
351                                 )
352                         )) {
353                                 $this->Session->setFlash(__('The new system has been saved', true));
354                                 Cache::clear();
355                                 $this->redirect(array('action'=>'view', $id));
356                         } else {
357                                 $this->Session->setFlash(__('The data could not be saved. Please, try again.', true));
358                         }
359                 }
360
361                 $this->set('isOwner', true);
362                 $this->set('character', $character);
363
364                 $systems = $this->_get_systems();
365                 $this->set('systems', $systems);
366         }
367
368
369         function set_status($id = null) {
370                 if (!$id || !isset($this->params['named']['mode'])) {
371                         $this->Session->setFlash(__('Invalid ID.', true));
372                         $this->redirect(array('controller' => 'characters', 'action'=>'index'));
373                 }
374
375                 $character = $this->_get_character4character_id($id, $this->user_id, 'public');
376
377                 if ($this->params['named']['mode'] != 'main_picture' && $this->params['named']['mode'] != 'full_length') {
378                         $this->Session->setFlash(__('Invalid URL.', true));
379                         $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
380                 }
381
382                 // 新picture設定
383                 $new_character_picture = null;
384                 if (isset($this->params['named']['new_picture_id']) && $this->params['named']['new_picture_id'] != 'null') {
385                         if ($character['CharacterPicture']) {
386                                 foreach($character['CharacterPicture'] as $k => $v) {
387                                         if ($v['id'] == $this->params['named']['new_picture_id']) {
388                                                 $new_character_picture = CorePlus::get_value($v, 'Attachment.0.basename');
389                                                 break;
390                                         }
391                                 }
392                         }
393                         if (!$new_character_picture) {
394                                 $this->Session->setFlash(__('Invalid data.', true));
395                                 $this->redirect(array('controller' => 'character_pictures', 'action'=>'index', $id));
396                         }
397                 }
398
399                 // 設定変更
400                 $this->Character->id = $id;
401                 $this->data['Character'][$this->params['named']['mode']] = $new_character_picture;
402                 $this->Character->save(
403                         $this->data, 
404                         array(
405                                 'fieldList' => array(
406                                         $this->params['named']['mode']
407                                 ), 
408                         )
409                 );
410
411                 $this->Session->setFlash(__('CharacterPicture Configuration has been saved.', true));
412                 $this->redirect(array('controller' => 'character_pictures', 'action' => 'index', $id));
413         }
414
415         function delete($id = null) {
416                 if (!$id) {
417                         $this->Session->setFlash(__('Invalid id for Character', true));
418                         $this->redirect(array('action'=>'index'));
419                 }
420
421                 $character = $this->Character->read(null, $id);
422                 if (empty($character)) {
423                         $this->Session->setFlash(__('No Character', true));
424                         $this->redirect(array('action'=>'index'));
425                 }
426
427                 if (!$this->isOwner($character['Character'], $this->user_id)) {
428                         $this->Session->setFlash(__('No Permission', true));
429                         $this->redirect(array('action'=>'index'));
430                 }
431
432                 if ($this->Character->del($id)) {
433                         $this->Session->setFlash(__('Character deleted', true));
434                         $this->redirect(array('action'=>'index'));
435                 }
436
437                 $this->set('isOwner', true);
438         }
439
440
441         function admin_index($id = null) {
442                 unset($this->Character->belongsTo['System']['conditions']['System.public_flag']);
443                 $this->_index($id, array('isAdmin' => true));
444         }
445
446         function admin_view($id = null) {
447                 if (!$id) {
448                         $this->Session->setFlash(__('Invalid Character.', true));
449                         $this->redirect(array('action' => 'admin_index'));
450                 }
451
452                 $character = $this->_view($id, array(), true);
453
454                 $this->set('character', $this->HtmlEscape->nl2br_escaped($character));
455         }
456
457         function admin_delete($id = null) {
458                 if (!$id) {
459                         $this->Session->setFlash(__('Invalid id for Character', true));
460                         $this->redirect(array('action'=>'index'));
461                 }
462                 if ($this->Character->del($id)) {
463                         $this->Session->setFlash(__('Character deleted', true));
464                         $this->redirect(array('action'=>'index'));
465                 }
466         }
467
468
469         /* 共通化アクションメソッド */
470         function _index($id, $conditions = array(), $limit = 20, $fields = array(), $contain = array(), $order = array(), $page = 1) {
471                 if (isset($conditions['isAdmin']) && $conditions['isAdmin'] === true) {
472                         $isAdmin = true;
473                 } else {
474                         $isAdmin = false;
475                 }
476                 unset($conditions['isAdmin']);
477
478                 // System指定分岐
479                 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
480                         //情報取得
481                         $conditions['System.id'] = $this->params['named']['system'];
482                         if ($isAdmin === false) {
483                                 $conditions['System.public_flag'] = 'public';
484                         }
485
486                         $this_system = $this->Character->System->find('first', array(
487                                 'conditions' => $conditions,
488                                 'recursive' => -1,
489                         ));
490
491                         if (!isset($this_system['System'])) {
492                                 $this->Session->setFlash(__('Invalid System.', true));
493                                 $this->redirect(array('controller' => 'systems', 'action' => 'index'));
494                         }
495                         $this_system = $this->_restore_html_system($this_system);
496
497                         $this->set('this_system', $this_system);
498                 } else {
499                         $contain = array_merge(array('System'), (array)$contain);
500                 }
501
502                 $isOwner = false;
503                 $user = array();
504
505                 $conditions = array('User.id' => $id);
506                 if (!empty($id)) {
507                         $user = $this->Character->User->find('first', array(
508                                 'conditions' => $conditions,
509                                 'recursive' => -1,
510                                 'fields' => array(
511                                         'User.id',
512                                         'User.name',
513                                 ),
514                         ));
515
516                         if (empty($user)) {
517                                 $this->Session->setFlash(__('Invalid Id.', true));
518                                 $this->redirect(array('action'=>'index'));
519                         }
520                 } else {
521                         $contain = array_merge($contain, array('User'));
522                 }
523                 $this->set('userSet', $user);
524
525                 if (!empty($user)) {
526                         if ($user['User']['id'] == $this->user_id) {
527                                 $isOwner = true;
528                         }
529                 }
530
531                 $conditions = array();
532                 if (!empty($id)) {
533                         $conditions['Character.user_id'] = $id;
534                 }
535                 if ($isAdmin === true) {
536                         $conditions['isAdmin'] = true;
537                 }
538                 if (isset($this->params['named']['system']) && intval($this->params['named']['system'])) {
539                         $conditions['Character.system_id'] = $this->params['named']['system'];
540                 }
541                  $characters =  $this->HtmlEscape->nl_unescape($this->_get_characters_page4user_id($id, $conditions, $limit, $fields, $contain, $order, $page));
542
543                 $this->set('characters', $characters);
544
545                 $this->set('isOwner', false);
546                 $this->set('isOwner_userSet', $isOwner);
547         }
548
549         function _view($id, $conditions = array(), $isAdmin = false) {
550                 $this->helpers[] = 'CharacterSheet';
551
552                 // キャラクターシート設定
553                 if (isset($this->params['named']['mode']) && !empty($this->params['named']['mode'])) {
554                          $this->view = 'Theme';
555                          $this->theme = $this->params['named']['mode'];
556                 }
557
558                 // キャラデータ取得
559                 $orig_character = $this->_get_character($id, $conditions, $isAdmin);
560
561                 $character = $this->Character->set_profiles2view($orig_character);
562
563                 // Systemチェック
564                 $this->set('systemValid', 'public');
565                 if (!$character['System']) {
566                         $this->set('systemValid', false);
567                 }
568                 if (!$this->check_public_flag($character['System'])) {
569                         $this->set('systemValid', 'unpublic');
570                 }
571                 $character = $this->_restore_html_system($character);
572
573
574                 return $character;
575         } 
576
577
578         /* 共通関数 */
579         function _get_character($id, $conditions = array(), $isAdmin = false)
580         {
581                 $character = $this->Character->get_character($id, $conditions, $isAdmin);
582
583                 if (empty($character)) {
584                         $this->Session->setFlash(__('No Character', true));
585                         $this->redirect(array('action'=>'index'));
586                 }
587
588                 if ($isAdmin === false && !$this->check_public_flag($character['Character'])) {
589                         $this->Session->setFlash(__('No Permission', true));
590                         $this->redirect(array('action'=>'index'));
591                 }
592
593                 return $character;
594         }
595
596         // ProfileTableをCharacterHasProfilesのTableの形式にセット
597         function _set_profile_table2characters_has_profiles($profile)
598         {
599                 if (!empty($profile[0]['CharactersHasProfile'])) {
600                         return $profile;
601                 }
602
603                 foreach($profile as $k => $v) {
604                         if (empty($v['ProfileTable'])) {
605                                 continue;
606                         }
607                         $tmp = array();
608                         $rows = count($v['CharactersHasProfile']);
609                         for($i=0; $i<$rows; $i++) {
610                                 foreach($v['ProfileTable'] as $k2 => $v2) {
611                                         if (isset($v2['CharactersHasProfile'][$i])) {
612                                                 $tmp[] = $v2['CharactersHasProfile'][$i];
613                                         } elseif (isset($v['CharactersHasProfile'][$i])) {
614                                                 $tmp[] = $v['CharactersHasProfile'][$i];
615                                                 break;
616
617                                         } else {
618                                                 $tmp[] = null;
619                                         }
620                                 }
621                         }
622                         $profile[$k]['CharactersHasProfile'] = $tmp;
623                         }
624
625                 return $profile;
626         }
627
628         /* POSTのCharactersHasProfileを処理 */
629         function _set_new_characters_has_profile($characters_has_profiles, $profile_id = null, $now_data = array())
630         {
631                 $prev_profile_id = null;
632                 $profileTable_tmp = array();
633                 $i = 0;
634                 foreach ($characters_has_profiles as $k => $v) {
635                         if (!empty($character_id)) {
636                                 $characters_has_profiles[$k]['character_id'] = $character_id;
637                         }
638                         // 配列valueの処理
639                         if (is_array($v['value'])) {
640                                 $characters_has_profiles[$k]['value'] = $v['value'][0];
641                         }
642
643                         // サニタイズ
644                         $characters_has_profiles[$k]['value'] = Sanitize::html($characters_has_profiles[$k]['value']);
645                         $characters_has_profiles[$k]['value'] = preg_replace('/[\\\n]/', '', $characters_has_profiles[$k]['value']);
646                         $characters_has_profiles[$k]['value'] = preg_replace('/[\\\r]/', '', $characters_has_profiles[$k]['value']);
647                         // textarea改行処理
648                         if (isset($v['is_textarea']) && $v['is_textarea'] == 1) {
649                                 $characters_has_profiles[$k]['value'] = str_replace(array("\n\r", '\n', "\r"), '<br />', $characters_has_profiles[$k]['value']);
650                                 $characters_has_profiles[$k]['value'] = str_replace("\\", '', $characters_has_profiles[$k]['value']);
651                         }
652                         $characters_has_profiles[$k]['value'] = Sanitize::stripAll($characters_has_profiles[$k]['value']);
653
654                         // 空の値処理
655                         // table: 一時保管して全項目空の場合行削除
656                         if (CorePlus::is_valid($v, 'profile_table_id')) {
657                                 if (isset($profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']])) {
658                                         $i++;
659                                 }
660                                 $profileTable_tmp[$v['profile_id']][$i][$v['profile_table_id']] = array($characters_has_profiles[$k], $k);
661                         // それ以外は削除
662                         } elseif (empty($v['value'])) {
663                                         unset($characters_has_profiles[$k]);
664                         }
665                 }
666
667                 // table: 空行削除、不正profile_table_idデータ削除
668                 if (!empty($profileTable_tmp)) {
669                         if (!isset($this->Character->ProfileTable)) {
670                                 $this->Character->ProfileTable = CorePlus::set_model('ProfileTable');
671                         }
672                         foreach($profileTable_tmp as $profile_id => $v) {
673                                 $profile_table_columns = $this->Character->ProfileTable->find('list', array(
674                                         'conditions' => array('ProfileTable.profile_id' => $profile_id),
675                                         'fields' => array('ProfileTable.id'),
676                                         'order' => array('ProfileTable.sort_order' => 'asc'),
677                                         'recursive' => -1,
678                                 ));
679
680                                 foreach ($v as $i => $profile_tables) {
681                                         // 不正POSTの削除
682                                         $diff_keys = array_diff_key($profile_tables, $profile_table_columns);
683                                         if (!empty($diff_keys)) {
684                                                 foreach($diff_keys as $dvalue) {
685                                                         unset($characters_has_profiles[$dvalue[1]]);
686                                                 }
687                                         }
688
689                                         // 空行削除
690                                         $delete_flg = true;
691                                         foreach($profile_table_columns as $k => $columns) {
692                                                 if (CorePlus::is_valid($profile_tables, $columns.'.0.value')) {
693                                                         $delete_flg = false;
694                                                         break;
695                                                 }
696                                         }
697                                         if ($delete_flg === true) {
698                                                 foreach ($profile_tables as $delk) {
699                                                         unset($characters_has_profiles[$delk[1]]);
700                                                 }
701                                         }
702                                 }
703                         }
704                 }
705
706                 // 指定データのみ変更
707                 if (!is_null($profile_id) && !empty($now_data)) {
708                         $tmp = array();
709                         $i = 0;
710                         foreach($now_data as $k => $v) {
711                                 if ($v['id'] == $profile_id) {
712                                         $v['CharactersHasProfile'] = $characters_has_profiles;
713                                 }
714
715                                 foreach ($v['CharactersHasProfile'] as $k2 => $v2) {
716                                         $tmp[$i] = $v2;
717                                         if (isset($tmp[$i]['id'])) {
718                                                 unset($tmp[$i]['id']);
719                                         }
720                                         if (isset($tmp[$i]['character_id'])) {
721                                                 unset($tmp[$i]['character_id']);
722                                         }
723
724                                         $i++;
725                                 }
726                         }
727
728                         $characters_has_profiles = $tmp;
729                 }
730
731                 return $characters_has_profiles;
732         }
733
734         // CharactersHasProfileの追加valiadte設定
735         function _set_validate4characters_has_profile($system_id)
736         {
737                 $this->Character->CharactersHasProfile->validate['profile_id']['validProfileId'] = array(
738                         'rule' => array('validProfileId', $system_id),
739                 );
740                 $this->Character->CharactersHasProfile->validate['profile_select_id']['validProfileSelectId'] = array(
741                         'rule' => array('validProfileSelectId', $system_id),
742                         'allowEmpty' => true,
743                 );
744                 $this->Character->CharactersHasProfile->validate['profile_table_id']['validProfiletableId'] = array(
745                 'rule' => array('validProfiletableId', $system_id),
746                 'allowEmpty' => true,
747                 );
748         }
749
750         /* restore_html */
751         function _restore_html_character($data, $nl2br = false) {
752                 $data['name'] = $this->{$this->modelClass}->restore_html($data['name'], false, false, false);
753                 $data['notes'] = $this->{$this->modelClass}->restore_html($data['notes'], false, false, false);
754                 if ($nl2br) {
755                         $data['notes'] = str_replace('<br />', "\n", $data['notes']);
756                 }
757
758                 return $data;
759         }
760
761         function _restore_html_characters_has_profiles($profile, $nl2br = false) {
762                 if (empty($profile)) {
763                         return null;
764                 }
765
766                 foreach($profile as $k => $v) {
767                         if (empty($v['CharactersHasProfile'])) {
768                                 continue;
769                         }
770                         $profile[$k]['CharactersHasProfile'] = $this->__restore_html_characters_has_profiles($v['CharactersHasProfile'], $nl2br, $v['profile_type']);
771                 }
772
773                 return $profile;
774         }
775         function __restore_html_characters_has_profiles($data, $nl2br = false, $profile_type = null) {
776                 if (empty($data)) {
777                         return null;
778                 }
779
780                 foreach ($data as $k => $v) {
781                         if (isset($v['value'])) {
782                                 $data[$k]['value'] = $this->{$this->modelClass}->restore_html($v['value'], false, false, false);
783                                 if ($nl2br && $profile_type == 'textarea') {
784                                         $data[$k]['value'] = str_replace('<br />', "\n", $data[$k]['value']);
785                                 }
786                         }
787                 }
788
789                 return $data;
790         }
791
792 }