OSDN Git Service

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